【发布时间】:2010-06-29 13:05:52
【问题描述】:
我想暂停 pthreads,但显然没有 pthread_suspend 之类的功能。我在某处阅读了有关使用互斥锁和条件暂停 pthread 的信息,并将其用作以下内容:
#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);
}
};
但我不明白为什么我们需要互斥锁和条件来挂起和恢复 pthread。不使用条件是否可以暂停和恢复?
【问题讨论】: