1

我想从另一个 pthread 中唤醒一个 pthread - 但要经过一段时间。我知道带有 pthread_cond_wait 的信号或 pthread_signal 可以用来唤醒另一个线程,但我看不到安排这个的方法。情况将是这样的:

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). 

安排唤醒的任何简单方法(不创建每秒唤醒的第 4 个线程并决定是否有线程 3 的预定条目唤醒)。如果队列中只有低优先级的消息,我真的不想频繁唤醒线程 3。此外,由于消息是突发的(比如单次突发中有 1000 条高优先级消息),我不想为每条消息唤醒线程 3。它确实减慢了速度(因为每次唤醒时都会进行大量其他处理)。

我正在使用 ubuntu 电脑。

4

3 回答 3

2

如何使用pthread_cond_t通过 pthread API 提供的对象?您可以在您的线程中共享这样一个对象,并让他们适当地对其进行操作。
生成的代码应如下所示:

/*
 * 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;
}

如果您希望线程 3pthread_cond_wait()在超时后退出阻塞调用,请考虑pthread_cond_timedwait()改用(仔细阅读手册,您提供的超时值是绝对时间,而不是您不想超过的时间量)。
如果超时,pthread_cond_timedwait()将返回ETIMEDOUT错误。

编辑:我跳过了锁定/解锁调用中的错误检查,不要忘记处理这个潜在的问题!

EDIT²:我稍微回顾了代码

于 2013-01-30T13:01:18.133 回答
0

为什么不将当前时间与之前的一次保存进行比较?

time_t last_uncond_wakeup = time(NULL);
time_t last_recv = 0;

while (1)
{
    if (recv())
    {
        // Do things
        last_recv = time(NULL);
    }

    // Possible other things

    time_t now = time(NULL);
    if ((last_recv != 0 && now - last_recv > 5) ||
        (now - last_uncond_wakeup > 60))
    {
        wake(thread3);
        last_uncond_wakeup = now;
        last_recv = 0;
    }
}
于 2013-01-30T12:36:20.610 回答
0

您可以让唤醒线程自行等待。在唤醒线程中:

pthread_mutex_lock(&lock);
if (!wakeup_scheduled) {
    wakeup_scheduled = 1;
    wakeup_time = time() + 5;
    pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&lock);

在等待线程中:

pthread_mutex_lock(&lock);
while (!wakeup_scheduled)
    pthread_cond_wait(&cond, &lock);
pthread_mutex_unlock(&lock);

sleep_until(wakeup_time);

pthread_mutex_lock(&lock);
wakeup_scheduled = 0;
pthread_mutex_unlock(&lock);
于 2013-01-30T12:44:01.957 回答