thread.cpp
来自「Linux 下的线程池的源代码。很好用!」· C++ 代码 · 共 58 行
CPP
58 行
#include "Thread.h"CThread::CThread(bool createsuspended, int priority =-1){ m_CreateSuspended = createsuspended; SetThreadState(issCreate); m_ThreadName = NULL; sem_init(&m_ThreadSemaphore,false,0);}CThread::~CThread(){ if(NULL != m_ThreadName) free(m_ThreadName); }/* About Suspend and Resume. POSIX does not support suspending/resuming a thread. Suspending a thread is considerd dangerous since it is not guaranteed where the thread would be suspend. It might be holding a lock, mutex or it might be inside a critical section. In order to simulate it in Linux we've used signals. To suspend, a thread SIGSTOP is sent and to resume, SIGCONT is sent. Note that this is Linux only i.e. according to POSIX if a thread receives SIGSTOP then the entire process is stopped. However Linux doesn't entirely exhibit the POSIX-mandated behaviour. If and when it fully complies with the POSIX standard then suspend and resume won't work.*/void CThread::Suspended(void){ SetThreadState(issSuspended); if(pthread_kill(m_ThreadID,SIGSTOP) !=0) SetErrCode(Error_ThreadSuspend); else SetErrCode(Error_ThreadSuccess); SetThreadState(issSuspended);}//send the signal SIGCONTvoid CThread::Resume(void){ SetThreadState(issRunnding);}//terminate the threadvoid CThread::Terminate(void){ }//set the thread to sleepvoid CThread::Sleep(void){}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?