📄 pthread.c++
字号:
}//-----------------------------------------------------------------------------//// Description: Get the thread's process id//// Use: public//size_t Thread::getProcessId() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); if(pd->idSet == false) return (size_t)(pthread_self()); return (size_t)(pd->tid);}//-----------------------------------------------------------------------------//// Description: Set the thread's processor affinity//// Use: public//int Thread::setProcessorAffinity(unsigned int cpunum){ PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); pd->cpunum = cpunum; if (pd->cpunum<0) return -1; #ifdef __sgi int status; pthread_attr_t thread_attr; status = pthread_attr_init( &thread_attr ); if(status != 0) { return status; } status = pthread_attr_setscope( &thread_attr, PTHREAD_SCOPE_BOUND_NP ); return status;#elif defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) if (pd->isRunning && Thread::CurrentThread()==this) { cpu_set_t cpumask; CPU_ZERO( &cpumask ); CPU_SET( pd->cpunum, &cpumask );#if defined(HAVE_PTHREAD_SETAFFINITY_NP) pthread_setaffinity_np (pthread_self(), sizeof(cpumask), &cpumask);#elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) sched_setaffinity( 0, sizeof(cpumask), &cpumask );#elif defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) sched_setaffinity( 0, &cpumask );#endif } return -1;#else return -1;#endif}//-----------------------------------------------------------------------------//// Description: Determine if the thread is running//// Use: public//bool Thread::isRunning() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); return pd->isRunning;}//-----------------------------------------------------------------------------//// Description: Start the thread.//// Use: public//int Thread::start() { int status; pthread_attr_t thread_attr; status = pthread_attr_init( &thread_attr ); if(status != 0) { return status; } PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); size_t defaultStackSize; pthread_attr_getstacksize( &thread_attr, &defaultStackSize); if(status != 0) { return status; } if(defaultStackSize < pd->stackSize) { pthread_attr_setstacksize( &thread_attr, pd->stackSize); if(status != 0) { return status; } } //------------------------------------------------------------------------- // Now get what we actually have... // pthread_attr_getstacksize( &thread_attr, &defaultStackSize); if(status != 0) { return status; } pd->stackSize = defaultStackSize; //------------------------------------------------------------------------- // Prohibit the stack size from being changed. // pd->stackSizeLocked = true;#ifdef ALLOW_PRIORITY_SCHEDULING status = pthread_attr_setinheritsched( &thread_attr, PTHREAD_EXPLICIT_SCHED ); pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);#endif // ] ALLOW_PRIORITY_SCHEDULING if(status != 0) { return status; } pd->threadStartedBlock.reset(); status = pthread_create(&(pd->tid), &thread_attr, ThreadPrivateActions::StartThread, static_cast<void *>(this)); // wait till the thread has actually started. pd->threadStartedBlock.block(); if(status != 0) { return status; } pd->idSet = true; return 0;}//-----------------------------------------------------------------------------//// Description: Alternate thread start routine.//// Use: public//int Thread::startThread(){ if (_prvData) return start(); else return 0;}//-----------------------------------------------------------------------------//// Description: Join the thread.//// Use: public//int Thread::detach() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); return pthread_detach(pd->tid);}//-----------------------------------------------------------------------------//// Description: Join the thread.//// Use: public//int Thread::join() { void *threadResult = 0; // Dummy var. PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); return pthread_join(pd->tid, &threadResult);}//-----------------------------------------------------------------------------//// Description: test the cancel state of the thread.//// Use: public//int Thread::testCancel() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); if(pthread_self() != pd->tid) return -1; pthread_testcancel(); return 0;}//-----------------------------------------------------------------------------//// Description: Cancel the thread.//// Use: public//int Thread::cancel() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); if (pd->isRunning) { pd->isCanceled = true; int status = pthread_cancel(pd->tid); return status; } return 0;}//-----------------------------------------------------------------------------//// Description: Disable cancelibility//// Use: public//int Thread::setCancelModeDisable() { return pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, 0 );}//-----------------------------------------------------------------------------//// Description: set the thread to cancel immediately//// Use: public//int Thread::setCancelModeAsynchronous() { int status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0); if(status != 0) return status; return pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0);}//-----------------------------------------------------------------------------//// Description: set the thread to cancel at the next convienent point.//// Use: public//int Thread::setCancelModeDeferred() { int status = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0); if(status != 0) return status; return pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0);}//-----------------------------------------------------------------------------//// Description: Set the thread's schedule priority (if able)//// Use: public//int Thread::setSchedulePriority(ThreadPriority priority) {#ifdef ALLOW_PRIORITY_SCHEDULING PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); pd->threadPriority = priority; if(pd->isRunning) return ThreadPrivateActions::SetThreadSchedulingParams(this); else return 0;#else return -1;#endif}//-----------------------------------------------------------------------------//// Description: Get the thread's schedule priority (if able)//// Use: public//int Thread::getSchedulePriority() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); return pd->threadPriority;}//-----------------------------------------------------------------------------//// Description: Set the thread's scheduling policy (if able)//// Use: public//int Thread::setSchedulePolicy(ThreadPolicy policy) {#ifdef ALLOW_PRIORITY_SCHEDULING PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); pd->threadPolicy = policy; if(pd->isRunning) return ThreadPrivateActions::SetThreadSchedulingParams(this); else return 0;#else return -1;#endif}//-----------------------------------------------------------------------------//// Description: Set the thread's scheduling policy (if able)//// Use: public//int Thread::getSchedulePolicy() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); return pd->threadPolicy;}//-----------------------------------------------------------------------------//// Description: Set the thread's desired stack size//// Use: public//int Thread::setStackSize(size_t stackSize) { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); if(pd->stackSizeLocked == true) return 13; // EACESS pd->stackSize = stackSize; return 0;}//-----------------------------------------------------------------------------//// Description: Get the thread's stack size.//// Use: public//size_t Thread::getStackSize() { PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData); return pd->stackSize;}//-----------------------------------------------------------------------------//// Description: Print the thread's scheduling information to stdout.//// Use: public//void Thread::printSchedulingInfo() { ThreadPrivateActions::PrintThreadSchedulingInfo(this);}//-----------------------------------------------------------------------------//// Description: Yield the processor//// Use: protected//int Thread::YieldCurrentThread(){#if defined(HAVE_PTHREAD_YIELD) pthread_yield(); return 0;#elif defined(HAVE_SCHED_YIELD) return sched_yield();#else return -1;#endif}// Description: sleep//// Use: public//int Thread::microSleep(unsigned int microsec){ return ::usleep(microsec);}//-----------------------------------------------------------------------------//// Description: Get the number of processors//int OpenThreads::GetNumberOfProcessors(){#if defined(__linux__) long ret = sysconf(_SC_NPROCESSORS_ONLN); if (ret == -1) return 0; return ret;#elif defined(__sun__) long ret = sysconf(_SC_NPROCESSORS_ONLN); if (ret == -1) return 0; return ret;#elif defined(__sgi) long ret = sysconf(_SC_NPROC_ONLN); if (ret == -1) return 0; return ret;#elif defined(__hpux) int ret = mpctl(MPC_GETNUMSPUS, 0, NULL); if (ret == -1) return 0; return ret;#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(__MACH__) uint64_t num_cpus = 0; size_t num_cpus_length = sizeof(num_cpus);#if defined(__FreeBSD__) sysctlbyname("hw.ncpu", &num_cpus, &num_cpus_length, NULL, 0); #else sysctlbyname("hw.activecpu", &num_cpus, &num_cpus_length, NULL, 0);#endif return num_cpus;#else return 1;#endif}int OpenThreads::SetProcessorAffinityOfCurrentThread(unsigned int cpunum){ if (cpunum<0) return -1; Thread::Init(); Thread* thread = Thread::CurrentThread(); if (thread) { return thread->setProcessorAffinity(cpunum); } else {#if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) cpu_set_t cpumask; CPU_ZERO( &cpumask ); CPU_SET( cpunum, &cpumask );#if defined(HAVE_PTHREAD_SETAFFINITY_NP) pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask);#elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) sched_setaffinity( 0, sizeof(cpumask), &cpumask );#elif defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) sched_setaffinity( 0, &cpumask );#endif#endif } return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -