ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since cb0e6de was
cb0e6de,
checked in by Thierry Delisle <tdelisle@…>, 8 years ago
|
Threads now use monitor semantics to wait until completion
|
-
Property mode set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | #include <fstream> |
---|
2 | #include <kernel> |
---|
3 | #include <monitor> |
---|
4 | #include <thread> |
---|
5 | |
---|
6 | static int global12, global23, global13; |
---|
7 | |
---|
8 | struct monitor_t { |
---|
9 | monitor_desc m; |
---|
10 | }; |
---|
11 | |
---|
12 | monitor_desc * get_monitor( monitor_t * this ) { |
---|
13 | return &this->m; |
---|
14 | } |
---|
15 | |
---|
16 | static monitor_t m1, m2, m3; |
---|
17 | |
---|
18 | void increment( monitor_t * mutex p1, monitor_t * mutex p2, int * value ) { |
---|
19 | *value += 1; |
---|
20 | } |
---|
21 | |
---|
22 | struct MyThread { |
---|
23 | thread_desc __thrd; |
---|
24 | int target; |
---|
25 | }; |
---|
26 | |
---|
27 | DECL_THREAD(MyThread); |
---|
28 | |
---|
29 | void ?{}( MyThread * this, int target ) { |
---|
30 | this->target = target; |
---|
31 | } |
---|
32 | |
---|
33 | void ^?{}( MyThread * mutex this ) {} |
---|
34 | |
---|
35 | void main( MyThread* this ) { |
---|
36 | for(int i = 0; i < 1000000; i++) { |
---|
37 | choose(this->target) { |
---|
38 | case 0: increment( &m1, &m2, &global12 ); |
---|
39 | case 1: increment( &m2, &m3, &global23 ); |
---|
40 | case 2: increment( &m1, &m3, &global13 ); |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | int main(int argc, char* argv[]) { |
---|
46 | processor p; |
---|
47 | { |
---|
48 | scoped(MyThread) * f[6]; |
---|
49 | for(int i = 0; i < 6; i++) { |
---|
50 | f[i] = ((scoped(MyThread) *)malloc()){ i % 3 }; |
---|
51 | } |
---|
52 | |
---|
53 | for(int i = 0; i < 6; i++) { |
---|
54 | delete( f[i] ); |
---|
55 | } |
---|
56 | } |
---|
57 | sout | global12 | global23 | global13 | endl; |
---|
58 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.