'It looks thread synchronization is missing a signal
I was testing thread synchronization with very simple example.
Here is the example code and results. I don't understand why I cannot see "data : 1" or [[data : 1]] from the result. Does it mean printData thread and printDataFormat thread does not receive the signal sent from increase thread at the line 23?
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <pthread.h>
7
8 pthread_mutex_t mutex;
9 pthread_cond_t cond;
10
11 static int32_t data = 0;
12
13 void *increase(void *arg)
14 {
15 int32_t ldata = 0;
16 while(1)
17 {
18 pthread_mutex_lock(&mutex);
19 data++;
20 if (data == (ldata + 1))
21 {
22 printf(">>data = %d\n", data);
23 pthread_cond_signal(&cond);
24 }
25 ldata++;
26 pthread_mutex_unlock(&mutex);
27 sleep(2);
28 }
29 }
30
31 void *printData(void *arg)
32 {
33 while(1)
34 {
35 pthread_mutex_lock(&mutex);
36 pthread_cond_wait(&cond, &mutex);
37 printf("data : %d\n", data);
38 pthread_mutex_unlock(&mutex);
39 }
40 }
41
42 void *printDataFormat(void *arg)
43 {
44 while(1)
45 {
46 pthread_mutex_lock(&mutex);
47 pthread_cond_wait(&cond, &mutex);
48 printf("[[data : %d]]\n", data);
49 pthread_mutex_unlock(&mutex);
50 }
51 }
52
53 int32_t main(void)
54 {
55 pthread_t thread1, thread2, thread3;
56
57 pthread_mutex_init(&mutex, NULL);
58 pthread_cond_init(&cond, NULL);
59
60 pthread_create(&thread1, NULL, increase, NULL);
61 pthread_create(&thread2, NULL, printData, NULL);
62 pthread_create(&thread3, NULL, printDataFormat, NULL);
63
64 pthread_join(thread1, NULL);
65 pthread_join(thread2, NULL);
66
67 return 0;
68 }
And the output of this example is
$ ./a.out
>>data = 1
>>data = 2
data : 2
>>data = 3
[[data : 3]]
>>data = 4
data : 4
>>data = 5
[[data : 5]]
>>data = 6
data : 6
I also tried a little different test using the code below.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <pthread.h>
7
8 pthread_mutex_t mutex;
9 pthread_cond_t cond;
10
11 static int32_t data = 0;
12
13 void *increase(void *arg)
14 {
15 int32_t ldata = 0;
16 while(1)
17 {
18 pthread_mutex_lock(&mutex);
19 data++;
20 if (data == 1)
21 {
22 printf(">>data = %d\n", data);
23 pthread_cond_signal(&cond);
24 }
25 ldata++;
26 pthread_mutex_unlock(&mutex);
27 sleep(2);
28 }
29 }
30
31 void *printData(void *arg)
32 {
33 while(1)
34 {
35 pthread_mutex_lock(&mutex);
36 pthread_cond_wait(&cond, &mutex);
37 printf("data : %d\n", data);
38 pthread_mutex_unlock(&mutex);
39 }
40 }
41
42 int32_t main(void)
43 {
44 pthread_t thread1, thread2, thread3;
45
46 pthread_mutex_init(&mutex, NULL);
47 pthread_cond_init(&cond, NULL);
48
49 pthread_create(&thread1, NULL, increase, NULL);
50 pthread_create(&thread2, NULL, printData, NULL);
51
52 pthread_join(thread1, NULL);
53 pthread_join(thread2, NULL);
54
55 return 0;
56 }
And the result is
$ ./a.out
>>data = 1
It also looks thread printData does not receive the signal from increase. Am I misunderstanding something? Or do I have something wrong in my example code?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
