gpt4 book ai didi

C pthread : How to wake it up after some time?

转载 作者:行者123 更新时间:2023-11-30 15:52:14 26 4
gpt4 key购买 nike

我想从另一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com