Last change
on this file since 50e04cc was
c26bea2a,
checked in by Peter A. Buhr <pabuhr@…>, 19 months ago
|
first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names
|
-
Property mode set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | #include <fstream.hfa> |
---|
2 | #include <thread.hfa> |
---|
3 | |
---|
4 | thread Worker { volatile int result; }; |
---|
5 | |
---|
6 | void main(Worker & this) { |
---|
7 | this.result = -10; |
---|
8 | for(50) { |
---|
9 | yield(); |
---|
10 | } |
---|
11 | this.result = 42; |
---|
12 | } |
---|
13 | |
---|
14 | int get_result( Worker & mutex this ) { |
---|
15 | return this.result; |
---|
16 | } |
---|
17 | |
---|
18 | thread OtherWorker { volatile int result; }; |
---|
19 | |
---|
20 | void main(OtherWorker & this) { |
---|
21 | this.result = -10; |
---|
22 | LOOP: for() { |
---|
23 | waitfor( ^?{} : this) { |
---|
24 | break LOOP; |
---|
25 | } |
---|
26 | or else { |
---|
27 | yield(); |
---|
28 | } |
---|
29 | } |
---|
30 | this.result = 27; |
---|
31 | } |
---|
32 | |
---|
33 | int get_result( OtherWorker & mutex this ) { |
---|
34 | return this.result; |
---|
35 | } |
---|
36 | |
---|
37 | int main(int argc, char* argv[]) { |
---|
38 | { |
---|
39 | Worker workers[17]; |
---|
40 | for(i; 17) { |
---|
41 | join( workers[i] ); |
---|
42 | int res = get_result( workers[i] ); |
---|
43 | if( res != 42 ) { |
---|
44 | sout | "Worker" | i | "got incorrect result:" | res; |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | sout | "All workers got 42"; |
---|
49 | |
---|
50 | { |
---|
51 | OtherWorker workers[17]; |
---|
52 | for(i; 17) { |
---|
53 | join( workers[i] ); |
---|
54 | int res = get_result( workers[i] ); |
---|
55 | if( res != 27 ) { |
---|
56 | sout | "Other Worker" | i | "got incorrect result:" | res; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | sout | "All other workers got 27"; |
---|
61 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.