我想暂停pthreads,但显然,没有像pthread_suspend这样的函数.我在某处读到了使用互斥锁和条件暂停pthreads并使用如下:
#include <pthread.h>
class PThread {
public:
pthread_t myPthread;
pthread_mutex_t m_SuspendMutex;
pthread_cond_t m_ResumeCond;
void start() {
pthread_create(&myPthread, NULL, threadRun, (void*)this );
}
Thread() { }
void suspendMe() {
pthread_cond_wait(&m_ResumeCond,&m_SuspendMutex);
}
void resume() {
pthread_cond_signal(&m_ResumeCond);
}
};
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么我们需要互斥和条件来暂停和恢复pthread.是否可以在不使用条件的情况下暂停和恢复它?