source: tests/concurrency/pthread/pthread_demo_create_join.cfa @ 523e300

Last change on this file since 523e300 was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 17 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: 877 bytes
RevLine 
[20be782]1#include <fstream.hfa>
2#include <thread.hfa>
[428adbc]3/* test pthread create/join/exit */
4
[20be782]5int arr[20];
6
7void* fetch(void* idx){
8    int res = arr[(uint64_t)idx];
9    pthread_exit((void*)res);
10    sout | "it should not be here";
11    exit(1);
12    //return (void*)res;
13}
14
15void arr_init(){
16    for (int i = 0; i < 20; i++){
17        arr[i] = i;
18    }
19}
20
21int main(int argc, char const *argv[])
22{
23    pthread_t threads[20];
24    arr_init();
25    int status;
26    for (int i = 0; i < 20; i++){
27        status = pthread_create(&threads[i], NULL, fetch, (void*)i);
28        if (status != 0) exit(1);
29    }
30    int res = 0;
31    for (int i = 0; i < 20; i++){
32        void* _res = NULL;
33        status = pthread_join(threads[i], &_res);
34        if (status != 0) exit(2);
35        if (((uint64_t)_res) != i) exit(3);
36        res += (uint64_t)_res;
37    }
38    sout | "final res is" | res;
39
40    return 0;
41}
Note: See TracBrowser for help on using the repository browser.