sle*_*eep 7 c++ linux 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.是否可以在不使用条件的情况下暂停和恢复它?
您的代码不正确 - pthread_cond_wait()要求在您调用互斥锁时已锁定互斥锁:
void suspendMe()
{
pthread_mutex_lock(&m_SuspendMutex);
pthread_cond_wait(&m_ResumeCond, &m_SuspendMutex);
pthread_mutex_unlock(&m_SuspendMutex);
}
Run Code Online (Sandbox Code Playgroud)
但是,这仍然是错误的.一个线程可以从醒来pthread_cond_wait()的任何名称,并不一定只有当它发出信号.这意味着您需要pthread_cond_wait()与某个共享状态配对,该状态编码线程正在等待的条件 - 在最简单的情况下,您可以只使用标志变量. pthread_cond_signal()用于告诉线程它应该唤醒并重新检查共享状态.将此应用于您的实现:
class PThread {
public:
pthread_t myPthread;
bool suspended;
pthread_mutex_t m_SuspendMutex;
pthread_cond_t m_ResumeCond;
void start() {
suspended = false;
pthread_create(&myPthread, NULL, threadRun, (void*)this );
}
Thread() { }
void suspendMe() {
pthread_mutex_lock(&m_SuspendMutex);
suspended = true;
do {
pthread_cond_wait(&m_ResumeCond, &m_SuspendMutex);
} while (suspended);
pthread_mutex_unlock(&m_SuspendMutex);
}
void resume() {
/* The shared state 'suspended' must be updated with the mutex held. */
pthread_mutex_lock(&m_SuspendMutex);
suspended = false;
pthread_cond_signal(&m_ResumeCond);
pthread_mutex_unlock(&m_SuspendMutex);
}
};
Run Code Online (Sandbox Code Playgroud)
提供互斥锁的原因是为了保护共享状态并避免竞争条件 - 该pthread_cond_wait()功能在等待时实际执行原子解锁和等待,这样可以避免"错过唤醒".例如,在此代码中,互斥锁防止suspended在suspended = true;和pthread_cond_wait()行之间更改为false .
如果一个线程没有在某种条件下等待,你怎么能"发信号"它恢复.它不能只是停止执行任何事情,然后再次神奇地重新开始,所以等待条件.
详细说明,在pthreads中,恢复线程的方法实际上是使用条件变量.没有可用于以任何其他方式挂起/恢复线程的API.等待pthread_cond_wait是便宜的,它会阻止,直到条件发出信号,而不是使用(多?)CPU.您使用条件来通知线程唤醒,并且需要互斥锁来保护对条件变量的访问以及唤醒时线程中的代码.
| 归档时间: |
|
| 查看次数: |
11107 次 |
| 最近记录: |