source: CIVL/examples/translation/pthread/sync01_true.cvl@ 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: 783 bytes
RevLine 
[5fb1a47]1#include <civlc.h>
2#include "pthread.cvh"
3
4int num;
5pthread_mutex_t m;
6pthread_cond_t empty, full;
7
8void *thread1(void*arg)
9{
10 pthread_mutex_lock(&m);
11
12 while (num > 0)
13 pthread_cond_wait(&empty, &m);
14
15 num++;
16
17 pthread_mutex_unlock(&m);
18 pthread_cond_signal(&full);
19}
20
21
22void *thread2(void*arg)
23{
24 pthread_mutex_lock(&m);
25
26 while (num == 0)
27 pthread_cond_wait(&full, &m);
28
29 num--;
30
31 pthread_mutex_unlock(&m);
32
33 pthread_cond_signal(&empty);
34}
35
36
37void main()
38{
39 pthread_t t1, t2;
40
41 num = 1;
42
43 pthread_mutex_init(&m, 0);
44 pthread_cond_init(&empty, 0);
45 pthread_cond_init(&full, 0);
46
47 pthread_create(&t1, 0, thread1, 0);
48 pthread_create(&t2, 0, thread2, 0);
49
50 pthread_join(t1, 0);
51 pthread_join(t2, 0);
52
53 if (num!=1)
54 {
55 $assert($false);
56 }
57}
Note: See TracBrowser for help on using the repository browser.