C pthread:一段时间后如何唤醒它?

时间:2013-01-30 12:30:13

标签: c pthreads signals

我想从另一个pthread唤醒一个pthread - 但过了一段时间。我知道信号或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电脑。

3 个答案:

答案 0 :(得分:1)

如何通过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错误。

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

EDIT²:我稍微审核了一下代码

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

你可以让woken线程自己做等待。在醒着的话题中:

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