source:
src/examples/thread.c
@
ad56482
Last change on this file since ad56482 was e15df4c, checked in by , 7 years ago | |
---|---|
|
|
File size: 927 bytes |
Rev | Line | |
---|---|---|
[8f49a54] | 1 | #include <fstream> |
[60819df7] | 2 | #include <kernel> |
[c49bf54] | 3 | #include <stdlib> |
4 | #include <threads> | |
5 | ||
[8f49a54] | 6 | // Start coroutine routines |
7 | struct MyThread { | |
[e15df4c] | 8 | thread t; |
[8f49a54] | 9 | unsigned id; |
10 | unsigned count; | |
11 | }; | |
[c49bf54] | 12 | |
[60819df7] | 13 | DECL_THREAD(MyThread) |
14 | ||
15 | void ?{}( MyThread * this ) { | |
16 | } | |
[c49bf54] | 17 | |
[8f49a54] | 18 | void ?{}( MyThread * this, unsigned id, unsigned count ) { |
19 | this->id = id; | |
20 | this->count = count; | |
21 | } | |
[c49bf54] | 22 | |
[60819df7] | 23 | void main(MyThread* this) { |
24 | sout | "Thread" | this->id | " : Suspending" | this->count | "times" | endl; | |
25 | yield(); | |
[c49bf54] | 26 | |
[60819df7] | 27 | for(int i = 0; i < this->count; i++) { |
28 | sout | "Thread" | this->id | " : Suspend No." | i + 1 | endl; | |
29 | yield(); | |
30 | } | |
31 | } | |
[c84e80a] | 32 | |
[8f49a54] | 33 | int main(int argc, char* argv[]) { |
[c49bf54] | 34 | |
[60819df7] | 35 | unsigned itterations = 10u; |
36 | if(argc == 2) { | |
37 | int val = ato(argv[1]); | |
38 | assert(val >= 0); | |
39 | itterations = val; | |
40 | } | |
[c49bf54] | 41 | |
[8f49a54] | 42 | sout | "User main begin" | endl; |
[c84e80a] | 43 | |
[60819df7] | 44 | { |
45 | processor p; | |
46 | { | |
[e15df4c] | 47 | scoped(MyThread) thread1 = { 1u, itterations }; |
48 | scoped(MyThread) thread2 = { 2u, itterations }; | |
[60819df7] | 49 | } |
50 | } | |
[c49bf54] | 51 | |
[8f49a54] | 52 | sout | "User main end" | endl; |
[c84e80a] | 53 | |
[c49bf54] | 54 | return 0; |
55 | } |
Note: See TracBrowser
for help on using the repository browser.