作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从另一个 pthread 中唤醒一个 pthread - 但需要一段时间。我知道 signal 或 pthread_signal 与 pthread_cond_wait 可用于唤醒另一个线程,但我看不到安排此操作的方法。情况大概是这样的:
THREAD 1:
========
while(1)
recv(low priority msg);
dump msg to buffer
THREAD 2:
========
while(1)
recv(high priority msg);
..do a little bit of processing with msg ..
dump msg to buffer
wake(THREAD3, 5-seconds-later); <-- **HOW TO DO THIS? **
//let some msgs collect for at least a 5 sec window.
//i.e.,Don't wake thread3 immediately for every msg rcvd.
THREAD 3:
=========
while(1)
do some stuff ..
Process all msgs in buffer
sleep(60 seconds).
任何简单的方法来安排唤醒(除了创建每秒唤醒一次并决定是否有线程 3 唤醒的计划条目的第四个线程)。如果队列中只有低优先级消息,我真的不想频繁唤醒线程 3。另外,由于消息是突发式的(比如一次突发中有 1000 条高优先级消息),我不想为每条消息唤醒线程 3。它确实减慢了速度(因为每次唤醒时它都会执行许多其他处理工作)。
我使用的是 ubuntu 电脑。
最佳答案
通过 pthread API 使用 pthread_cond_t
对象怎么样?您可以在线程中共享这样的对象,并让它们对其进行适当的操作。
生成的代码应如下所示:
/*
* I lazily chose to make it global.
* You could dynamically allocate the memory for it
* And share the pointer between your threads in
* A data structure through the argument pointer
*/
pthread_cond_t cond_var;
pthread_mutex_t cond_mutex;
int wake_up = 0;
/* To call before creating your threads: */
int err;
if (0 != (err = pthread_cond_init(&cond_var, NULL))) {
/* An error occurred, handle it nicely */
}
if (0 != (err = pthread_mutex_init(&cond_mutex, NULL))) {
/* Error ! */
}
/*****************************************/
/* Within your threads */
void *thread_one(void *arg)
{
int err = 0;
/* Remember you can embed the cond_var
* and the cond_mutex in
* Whatever you get from arg pointer */
/* Some work */
/* Argh ! I want to wake up thread 3 */
pthread_mutex_lock(&cond_mutex);
wake_up = 1; // Tell thread 3 a wake_up rq has been done
pthread_mutex_unlock(&cond_mutex);
if (0 != (err = pthread_cond_broadcast(&cond_var))) {
/* Oops ... Error :S */
} else {
/* Thread 3 should be alright now ! */
}
/* Some work */
pthread_exit(NULL);
return NULL;
}
void *thread_three(void *arg)
{
int err;
/* Some work */
/* Oh, I need to sleep for a while ...
* I'll wait for thread_one to wake me up. */
pthread_mutex_lock(&cond_mutex);
while (!wake_up) {
err = pthread_cond_wait(&cond_var, &cond_mutex);
pthread_mutex_unlock(&cond_mutex);
if (!err || ETIMEDOUT == err) {
/* Woken up or time out */
} else {
/* Oops : error */
/* We might have to break the loop */
}
/* We lock the mutex again before the test */
pthread_mutex_lock(&cond_mutex);
}
/* Since we have acknowledged the wake_up rq
* We set "wake_up" to 0. */
wake_up = 0;
pthread_mutex_unlock(&cond_mutex);
/* Some work */
pthread_exit(NULL);
return NULL;
}
如果您希望线程 3 在超时后退出对 pthread_cond_wait()
的阻塞调用,请考虑使用 pthread_cond_timedwait()
(仔细阅读该手册,超时您提供的值是绝对时间,而不是您不想超过的时间。
如果超时,pthread_cond_timedwait()
将返回 ETIMEDOUT
错误。
编辑:我跳过了锁定/解锁调用中的错误检查,不要忘记处理这个潜在的问题!
编辑²:我稍微检查了一下代码
关于C pthread : How to wake it up after some time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604060/
我是一名优秀的程序员,十分优秀!