ADTast-experimentalpthread-emulation
Last change
on this file since 20be782 was
20be782,
checked in by z277zhu <z277zhu@…>, 2 years ago
|
add pthread
|
-
Property mode set to
100644
|
File size:
912 bytes
|
Line | |
---|
1 | #include <fstream.hfa> |
---|
2 | #include <thread.hfa> |
---|
3 | /* test that threads can create successfully and join correct result */ |
---|
4 | int arr[20]; |
---|
5 | |
---|
6 | void* fetch(void* idx){ |
---|
7 | int res = arr[(uint64_t)idx]; |
---|
8 | pthread_exit((void*)res); |
---|
9 | sout | "it should not be here"; |
---|
10 | exit(1); |
---|
11 | //return (void*)res; |
---|
12 | } |
---|
13 | |
---|
14 | void arr_init(){ |
---|
15 | for (int i = 0; i < 20; i++){ |
---|
16 | arr[i] = i; |
---|
17 | } |
---|
18 | } |
---|
19 | |
---|
20 | int main(int argc, char const *argv[]) |
---|
21 | { |
---|
22 | pthread_t threads[20]; |
---|
23 | arr_init(); |
---|
24 | int status; |
---|
25 | for (int i = 0; i < 20; i++){ |
---|
26 | status = pthread_create(&threads[i], NULL, fetch, (void*)i); |
---|
27 | if (status != 0) exit(1); |
---|
28 | } |
---|
29 | int res = 0; |
---|
30 | for (int i = 0; i < 20; i++){ |
---|
31 | void* _res = NULL; |
---|
32 | status = pthread_join(threads[i], &_res); |
---|
33 | if (status != 0) exit(2); |
---|
34 | if (((uint64_t)_res) != i) exit(3); |
---|
35 | res += (uint64_t)_res; |
---|
36 | } |
---|
37 | sout | "final res is" | res; |
---|
38 | |
---|
39 | return 0; |
---|
40 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.