multithreading - In pthread, how to reliably pass signal to another thread? -


I am trying to write a simple thread pool program in pthread. However, it seems that pthread_cond_signal does not block, which creates a problem. For example, suppose I have a "manufacturer-consumer" program:

 < Code> pthread_cond_t my_cond = PTHREAD_COND_INITIALIZER; Pthread_mutex_t my_cond_m = PTHREAD_MUTEX_INITIALIZER; Zero * Savior (Zero * Arg) {// XXX Make sure that he is ready to be free (1); Pthread_mutex_lock (& ​​my_cond_m); Pthread_cond_signal (& my_cond); Pthread_mutex_unlock (& ​​my_cond_m); Return tap; } Int main () {pthread_t t1; Pthread_create (& amp; t1, NULL, Savior, NULL); Do not be too long to get ready // // XXX. Otherwise, I would remember that always sleep // (3) to wake up; Pthread_mutex_lock (& ​​my_cond_m); Pthread_cond_wait (& my_cond, & my_cond_m); Pthread_mutex_unlock (& ​​my_cond_m); Pthread_join (t1, NULL); Return 0; } If I remove the  Sleep  call, then the  main , as specified in the two  XXX  digits  

Stall can be because it lost the awake call from the liberator () . Of course, sleep is not a very strong way to make sure that either

In the real life situation, this manager says to the thread that he is ready for it


uplift

@Bohriad works in the north , Or manager Thread has announced that new work is available.

As a work, but their explanation of the problem may be better, I look at this question and ask anyone to read what is going on to read the discussion in the comments.

Specifically, I will modify such an answer and code example myself, to make it clear. (Since the original answer from Boridies, compiling and working, I was very confused)

  // in main pthread_mutex_lock (& ​​my_cond_m); // If the flag is not set, this means that the liberator has not yet ran //. I'm waiting for him through the pthread signal // Tantra // if it sets _is_, that means that the libeller is running. I will just wait / wait because I have already synchronized. I do not need to use the / pthread signaling mechanism if (! Flag) pthread_cond_wait (& my_cond, & amp; my_cond_m); Pthread_mutex_unlock (& ​​my_cond_m); // pthread_mutex_lock in the liberator thread (& my_cond_m); // Any sleep signal If no one is still asleep, // should check this flag, which indicates that I have already sent the // signal. This is necessary because pthread signal // is not like a message queue - a sent signal is lost if // does not send a condition when it is sent. // You can think of this flag as a "constant" signal flag = 1; Pthread_cond_signal (& my_cond); Pthread_mutex_unlock (& ​​my_cond_m);  

Use a synchronization variable.

In the main :

  pthread_mutex_lock (and my_cond_m); While (! Flag) {pthread_cond_wait (& my_cond, & amp; my_cond_m); } Pthread_mutex_unlock (& ​​my_cond_m);  

In thread:

  pthread_mutex_lock (& ​​my_cond_m); Flag = 1; Pthread_cond_broadcast (& my_cond); Pthread_mutex_unlock (& ​​my_cond_m);  

For the consumer-consumer problem, this consumer may have been sleeping when the buffer is free, and the productor sleeps completely.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -