pthread_cond_broadcast & pthread_cond_signal

Role pthread_cond_broadcast (& cond1) is to wake up all being pthread_cond_wait (& cond1, & mutex1) thread.

pthread_cond_signal (& cond1) of the role is to wake up all being pthread_cond_wait (& cond1, & mutex1) of one of the threads.

 

Divided into the following discussions about the effects of these two functions.

The first case: multiple threads waiting for the same cond, and want the same for a mutex lock.

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 
 6 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 7 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
 8 
 9 void* thread_task1(void* arg)
10 {
11     pthread_mutex_lock(&mutex1);
12 
13     pthread_cond_wait(&cond,&mutex1);
14 
15     printf("thread_task1 start working\n");
16     sleep(2);
17     printf("thread_task1 works over\n");
18     pthread_mutex_unlock(&mutex1);
19 
20     return NULL;
21 
22 
23 }
24 
25 void* thread_task2(void* arg)
26 {
27     pthread_mutex_lock(&mutex1);
28 
29     pthread_cond_wait(&cond,&mutex1);
30 
31     printf("thread_task2 start working\n");
32     sleep(2);
33     printf("thread_task2 works over\n");
34     pthread_mutex_unlock(&mutex1);
35 
36     return NULL;
37 
38 }
39 
40 void* broadcastSameMutex(void* arg)
41 {
42     pthread_cond_broadcast(&cond);
43     return NULL;
44 }
45 
46 void* signalSameMutex(void* arg)
47 {
48     pthread_cond_signal(&cond);
49     return NULL;
50 }
51 
52 int main()
53 {
54     pthread_t thread_1,thread_2,thread_3;
55     pthread_create(&thread_1,NULL,thread_task1,NULL);
56     pthread_create(&thread_2,NULL,thread_task2,NULL);
57     sleep(2);
58 
59 #ifdef SIGNAL
60     pthread_create(&thread_3,NULL,signalSameMutex,NULL);
61 #else
62     pthread_create(&thread_3,NULL,broadcastSameMutex,NULL);
63 #endif
64 
65 
66     pthread_join(thread_1,NULL);
67     pthread_join(thread_2,NULL);
68     pthread_join(thread_3,NULL);
69 
70     pthread_mutex_destroy(&mutex1);
71     pthread_cond_destroy(&cond);
72     return 0;
73 
74 }

 

Use of broadcast operating results:

 

 

 Use of operating results signal:

analysis:

  1. When using broadcast mode, two blocked threads are awakened, thread to be awakened first thing to want to do mutex1 locked, in the course of this operation thread_1 lock succeeded, though without success thread_2 grab the lock, but it has not been idle, it has been trying to mutex1 be locked, heaven pays off, after thread_1 finished release the lock, thread_2 successfully got the lock, and then successfully implemented.
  2. When a signal mode, thread_1 and thread_2 only wakes up a thread, in this run is thread_1 been awakened, but because thread_2 not awake, so it has been blocked in pthread_cond_wait place, so in the end only thread_1 finished .

 

 

 The second case: multiple threads waiting for the same cond, and are different mutex lock.

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <pthread.h>
 4 #include <stdlib.h>
 5 
 6 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 7 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
 8 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
 9 
10 void* thread_task1(void* arg)
11 {
12     pthread_mutex_lock(&mutex1);
13 
14     pthread_cond_wait(&cond,&mutex1);
15 
16     printf("thread_task1 start working\n");
17     sleep(2);
18     printf("thread_task1 works over\n");
19     pthread_mutex_unlock(&mutex1);
20 
21     return NULL;
22 
23 
24 
25 }
26 
27 void* thread_task2(void* arg)
28 {
29     pthread_mutex_lock(&mutex2);
30 
31     pthread_cond_wait(&cond,&mutex2);
32 
33     printf("thread_task2 start working\n");
34     sleep(2);
35     printf("thread_task2 works over\n");
36     pthread_mutex_unlock(&mutex2);
37 
38     return NULL;
39 
40 
41 }
42 
43 void* broadcastDiffMutex(void* arg)
44 {
45     pthread_cond_broadcast(&cond);
46     return NULL;
47 
48 }
49 
50 void* signalDiffMutex(void* arg)
51 {
52     pthread_cond_signal(&cond);
53     return NULL;
54 
55 }
56 
57 int main()
58 {
59     pthread_t thread_1,thread_2,thread_3;
60     pthread_create(&thread_1,NULL,thread_task1,NULL);
61     pthread_create(&thread_2,NULL,thread_task2,NULL);
62     sleep(2);
63 
64 #ifdef SIGNAL
65     pthread_create(&thread_3,NULL,signalDiffMutex,NULL);
66 #else
67     pthread_create(&thread_3,NULL,broadcastDiffMutex,NULL);
68 #endif
69 
70 
71     pthread_join(thread_1,NULL);
72     pthread_join(thread_2,NULL);
73     pthread_join(thread_3,NULL);
74 
75     pthread_mutex_destroy(&mutex1);
76     pthread_mutex_destroy(&mutex2);
77     pthread_cond_destroy(&cond);
78     return 0;
79 
80 }

 

The effect of the use of broadcast:

 

Use the effect signal

 

 analysis:

  1. When using broadcast mode, because two threads are awakened, and they want to add locks and no competition, so they are executed concurrently, rather than after performing as a front in the former case as a must.
  2. When a signal way, only to be awakened with a thread, so that only one thread executed successfully.

  

Guess you like

Origin www.cnblogs.com/XiaoXiaoShuai-/p/11855408.html