📄 win32thread.cpp
字号:
// wait till the thread has actually started. pd->threadStartedBlock.block(); if(!pd->tid) { return -1; } return 0;}int Thread::startThread(){ if (_prvData) return start(); else return 0;}//-----------------------------------------------------------------------------//// Description: Join the thread.//// Use: public//int Thread::join() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); if( pd->detached ) return -1; // cannot wait for detached ; if( WaitForSingleObject(pd->tid.get(),INFINITE) != WAIT_OBJECT_0) return -1 ; return 0;}int Thread::detach(){ Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); pd->detached = true; return 0;}//-----------------------------------------------------------------------------//// Description: Cancel the thread.//// Use: public//int Thread::cancel(){ Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); if (pd->isRunning) { if( pd->cancelMode == 2 ) return -1; // signal all interested parties that we are going to exit SetEvent(pd->cancelEvent.get()); // cancelMode == 1 (asynch)-> kill em // cancelMode == 0 (deffered) -> wait a little then kill em // if( (pd->cancelMode == 1) || (WaitForSingleObject(pd->tid,INFINITE)!=WAIT_OBJECT_0) ) if( pd->cancelMode == 1 ) { // did not terminate cleanly force termination pd->isRunning = false; return TerminateThread(pd->tid.get(),(DWORD)-1); } } return 0;}int Thread::testCancel(){ Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); if(WaitForSingleObject(pd->cancelEvent.get(),0) != WAIT_OBJECT_0) return 0; if(pd->cancelMode == 2) return 0; DWORD curr = GetCurrentThreadId(); if( pd->uniqueId != (int)curr ) return -1;// pd->isRunning = false;// ExitThread(0); throw Win32ThreadCanceled(); return 0;}//-----------------------------------------------------------------------------//// Description: Disable cancelibility//// Use: public//int Thread::setCancelModeDisable() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); pd->cancelMode = 2; return 0;}//-----------------------------------------------------------------------------//// Description: set the thread to cancel immediately//// Use: public//int Thread::setCancelModeAsynchronous() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); pd->cancelMode = 1; return 0;}//-----------------------------------------------------------------------------//// Description: set the thread to cancel at the next convenient point.//// Use: public//int Thread::setCancelModeDeferred() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); pd->cancelMode = 0; return 0;}//-----------------------------------------------------------------------------//// Description: Set the thread's schedule priority (if able)//// Use: public//int Thread::setSchedulePriority(ThreadPriority priority) { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); pd->threadPriority = priority; if(pd->isRunning) return ThreadPrivateActions::SetThreadSchedulingParams(this); else return 0;}//-----------------------------------------------------------------------------//// Description: Get the thread's schedule priority (if able)//// Use: public//int Thread::getSchedulePriority() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); return pd->threadPriority;}//-----------------------------------------------------------------------------//// Description: Set the thread's scheduling policy (if able)//// Use: public//int Thread::setSchedulePolicy(ThreadPolicy policy) { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); pd->threadPolicy = policy; if(pd->isRunning) return ThreadPrivateActions::SetThreadSchedulingParams(this); else return 0;}//-----------------------------------------------------------------------------//// Description: Set the thread's scheduling policy (if able)//// Use: public//int Thread::getSchedulePolicy() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); return pd->threadPolicy;}//-----------------------------------------------------------------------------//// Description: Set the thread's desired stack size//// Use: public//int Thread::setStackSize(size_t stackSize) { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); if(pd->isRunning) return 13; // cannot set stack size of running thread return EACESS pd->stackSize = stackSize; return 0;}//-----------------------------------------------------------------------------//// Description: Get the thread's stack size.//// Use: public//size_t Thread::getStackSize() { Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); return pd->stackSize;}//-----------------------------------------------------------------------------//// Description: set processor affinity for the thread//// Use: public//int Thread::setProcessorAffinity( unsigned int cpunum ){ Win32ThreadPrivateData *pd = static_cast<Win32ThreadPrivateData *> (_prvData); DWORD affinityMask = 0x1 << cpunum ; // thread affinity mask DWORD_PTR res = SetThreadAffinityMask ( pd->tid.get(), // handle to thread affinityMask // thread affinity mask );/* This one is funny. This is "non-mandatory" affinity , windows will try to use dwIdealProcessor whenever possible ( when Bill's account is over 50B, maybe :-) ). DWORD SetThreadIdealProcessor( HANDLE hThread, // handle to the thread DWORD dwIdealProcessor // ideal processor number );*/ // return value 1 means call is ignored ( 9x/ME/SE ) if( res == 1 ) return -1; // return value 0 is failure return (res == 0) ? GetLastError() : 0 ;}//-----------------------------------------------------------------------------//// Description: Print the thread's scheduling information to stdout.//// Use: public//void Thread::printSchedulingInfo() { ThreadPrivateActions::PrintThreadSchedulingInfo(this);}//-----------------------------------------------------------------------------//// Description: Yield the processor//// Use: protected//#if _WIN32_WINNT < 0x0400 // simulateint SwitchToThread (void){ ::Sleep(10); return 0;};#endifint Thread::YieldCurrentThread(){ return SwitchToThread();}int Thread::microSleep(unsigned int microsec){#if _WIN32_WINNT < 0x0400 // simulate ::Sleep(microsec/1000); return 0;#else HandleHolder sleepTimer(CreateWaitableTimer(NULL, TRUE, NULL)); if( !sleepTimer ) return -1; LARGE_INTEGER t; t.QuadPart= -(LONGLONG)microsec*10; // in 100ns units // negative sign means relative, if (!SetWaitableTimer(sleepTimer.get(), &t, 0, NULL, NULL, 0)) { return -1; } // Wait for the timer. if (WaitForSingleObject(sleepTimer.get(), INFINITE) != WAIT_OBJECT_0) { return -1; } return 0;#endif}//-----------------------------------------------------------------------------//// Description: Get the number of processors//int OpenThreads::GetNumberOfProcessors(){ SYSTEM_INFO sysInfo; GetSystemInfo(&sysInfo); return sysInfo.dwNumberOfProcessors;}int OpenThreads::SetProcessorAffinityOfCurrentThread(unsigned int cpunum){ if (cpunum<0) return -1; Thread::Init(); Thread* thread = Thread::CurrentThread(); if (thread) { return thread->setProcessorAffinity(cpunum); } else { // non op right now, needs implementation. return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -