G.A.
G.A.

Reputation: 1523

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

I would like to wake up a pthread from another pthread - but after some time. I know signal or pthread_signal with pthread_cond_wait can be used to wake another thread, but I can't see a way to schedule this. The situation would be something like:

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

Any simple way to schedule a wakeup (short of creating a 4th thread that wakes up every second and decides if there is a scheduled entry for thread-3 to wakeup). I really don't want to wakeup thread-3 frequently if there are only low priority msgs in queue. Also, since the messages come in bursts (say 1000 high priority messages in a single burst), I don't want to wake up thread-3 for every single message. It really slows things down (as there is a bunch of other processing stuff it does every time it wakes up).

I am using an ubuntu pc.

Upvotes: 1

Views: 3879

Answers (3)

Rerito
Rerito

Reputation: 6086

How about the use of the pthread_cond_t object available through the pthread API ? You could share such an object within your threads and let them act on it appropriately.
The resulting code should look like this :

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

If you want your thread 3 to exit the blocking call to pthread_cond_wait() after a timeout, consider using pthread_cond_timedwait() instead (read the man carefully, the timeout value you supply is the ABSOLUTE time, not the amount of time you don't want to exceed).
If the timeout expires, pthread_cond_timedwait() will return an ETIMEDOUT error.

EDIT : I skipped error checking in the lock / unlock calls, don't forget to handle this potential issue !

EDIT² : I reviewed the code a little bit

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409136

Why not just compare the current time to one save earlier?

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

Upvotes: 0

caf
caf

Reputation: 239011

You can have the woken thread do the wait itself. In the waking thread:

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

In the waiting thread:

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

Upvotes: 0

Related Questions