小编sle*_*eep的帖子

不使用条件暂停pthreads

我想暂停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.是否可以在不使用条件的情况下暂停和恢复它?

c++ linux pthreads

7
推荐指数
2
解决办法
1万
查看次数

中断sleep()函数

如何使用信号处理程序中断sleep()?

c++

7
推荐指数
1
解决办法
6723
查看次数

标签 统计

c++ ×2

linux ×1

pthreads ×1