Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question [SOLVED] pthread : pthread_cond_wait


    Hello All,
    I was studying pthreads today.
    If multiple threads operate on a single shared resource, we can lock it using pthread_mutex_lock. Uptill that it is fine, but why again pthread_cond_wait?

    int shared=0; // global
    .
    .
    // Thread 1
    pthread_mutex_lock(&mutex);
    shared=1;
    pthread_mutex_unlock(&mutex);

    When thread1 invokes the mutex lock, no other threads can access it in parallel. So why again and what for we use, pthread_cond_wait( ). Someone kindly help me with a good code example.

  2. #2
    pthread_cond_wait is used in cases where one thread needs to wait for another thread to complete a job in order to proceed. In such cases you don't want to be sleeping and polling a state, you simply want to be notified when things are done and you can proceed.

    This function also with pthread_cond_timedwait and be used to implement a message queue for message passing between threads. In an event driven software design architecture.

  3. #3

    Thumbs up

    Quote Originally Posted by abienkowski View Post
    pthread_cond_wait is used in cases where one thread needs to wait for another thread to complete a job in order to proceed. In such cases you don't want to be sleeping and polling a state, you simply want to be notified when things are done and you can proceed.

    This function also with pthread_cond_timedwait and be used to implement a message queue for message passing between threads. In an event driven software design architecture.
    Thanks, this solves.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •