This repository was archived by the owner on Sep 29, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <pthread.h>
2
+ #include <semaphore.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+
6
+ #define NITER 1000000
7
+
8
+ int cnt = 0 ;
9
+
10
+ void * Count (void * a )
11
+ {
12
+ int i , tmp ;
13
+ for (i = 0 ; i < NITER ; i ++ )
14
+ {
15
+ tmp = cnt ; /* copy the global cnt locally */
16
+ tmp = tmp + 1 ; /* increment the local copy */
17
+ cnt = tmp ; /* store the local value into the global cnt */
18
+ }
19
+ }
20
+
21
+ int main (int argc , char * argv [])
22
+ {
23
+ pthread_t tid1 , tid2 ;
24
+
25
+ if (pthread_create (& tid1 , NULL , Count , NULL ))
26
+ {
27
+ printf ("\n ERROR creating thread 1" );
28
+ exit (1 );
29
+ }
30
+
31
+ if (pthread_create (& tid2 , NULL , Count , NULL ))
32
+ {
33
+ printf ("\n ERROR creating thread 2" );
34
+ exit (1 );
35
+ }
36
+
37
+ if (pthread_join (tid1 , NULL )) /* wait for the thread 1 to finish */
38
+ {
39
+ printf ("\n ERROR joining thread" );
40
+ exit (1 );
41
+ }
42
+
43
+ if (pthread_join (tid2 , NULL )) /* wait for the thread 2 to finish */
44
+ {
45
+ printf ("\n ERROR joining thread" );
46
+ exit (1 );
47
+ }
48
+
49
+ if (cnt < 2 * NITER )
50
+ printf ("\n BOOM! cnt is [%d], should be %d\n" , cnt , 2 * NITER );
51
+ else
52
+ printf ("\n OK! cnt is [%d]\n" , cnt );
53
+
54
+ pthread_exit (NULL );
55
+ }
56
+
57
+
58
+
You can’t perform that action at this time.
0 commit comments