source: CIVL/examples/translation/pthread/sync01_true.cvl@ bfebb46

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

added comments to pthread examples.

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

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