Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:38:53+00:00 2026-06-18T02:38:53+00:00

I would like to wake up a pthread from another pthread – but after

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:38:54+00:00Added an answer on June 18, 2026 at 2:38 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to know how to hide an div after a set of css3
I would like my app to wake up after it has been updated using
I would like to know, if it is possible to somehow wake up a
I reboot a remote computer with Wake-on-Lan and I would like to know if
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
Would like to know how to integarate cruise control with maven? Cruise Control comes
I'm debugging a deadlock. I would like to wake up a thread that's waiting
I would like to create an Android application with real-time monitoring functions. One monitoring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.