source: CIVL/examples/translation/pthread/sync01_true.c@ be4355b

1.23 2.0 main test-branch
Last change on this file since be4355b was 5fb1a47, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added pthread examples

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@818 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100755
File size: 812 bytes
Line 
1#include <stdio.h>
2#include <pthread.h>
3
4int num;
5
6pthread_mutex_t m;
7pthread_cond_t empty, full;
8
9void * thread1(void * arg)
10{
11 pthread_mutex_lock(&m);
12
13 while (num > 0)
14 pthread_cond_wait(&empty, &m);
15
16 num++;
17
18 pthread_mutex_unlock(&m);
19 pthread_cond_signal(&full);
20}
21
22
23void * thread2(void * arg)
24{
25 pthread_mutex_lock(&m);
26
27 while (num == 0)
28 pthread_cond_wait(&full, &m);
29
30 num--;
31
32 pthread_mutex_unlock(&m);
33
34 pthread_cond_signal(&empty);
35}
36
37
38int main()
39{
40 pthread_t t1, t2;
41
42 num = 1;
43
44 pthread_mutex_init(&m, 0);
45 pthread_cond_init(&empty, 0);
46 pthread_cond_init(&full, 0);
47
48 pthread_create(&t1, 0, thread1, 0);
49 pthread_create(&t2, 0, thread2, 0);
50
51 pthread_join(t1, 0);
52 pthread_join(t2, 0);
53
54 if (num!=1)
55 {
56 ERROR: goto ERROR;
57 ;
58 }
59
60 return 0;
61
62}
Note: See TracBrowser for help on using the repository browser.