1 | // tested pthread once,create,join |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | #include <fstream.hfa> |
---|
7 | #include <thread.hfa> |
---|
8 | #define THREADS 20 |
---|
9 | |
---|
10 | extern "C"{ |
---|
11 | #include <pthread.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <errno.h> |
---|
14 | int once_counter=0; |
---|
15 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
---|
16 | |
---|
17 | void once_fn(void) |
---|
18 | { |
---|
19 | puts("in once_fn"); |
---|
20 | once_counter++; |
---|
21 | } |
---|
22 | |
---|
23 | void *threadfunc(void *parm) |
---|
24 | { |
---|
25 | int status; |
---|
26 | int threadnum; |
---|
27 | int *tnum; |
---|
28 | |
---|
29 | tnum = (int *)parm; |
---|
30 | threadnum = *tnum; |
---|
31 | |
---|
32 | //printf("Thread %d executing\n", threadnum); |
---|
33 | |
---|
34 | status = pthread_once(&once_control, once_fn); |
---|
35 | if ( status < 0) |
---|
36 | printf("pthread_once failed, thread %d, errno=%d\n", threadnum, |
---|
37 | errno); |
---|
38 | |
---|
39 | //pthread_exit((void *)0); |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | void once_rtn(){ |
---|
45 | printf("in once init\n"); |
---|
46 | } |
---|
47 | void test(){ |
---|
48 | |
---|
49 | processor p[10]; |
---|
50 | |
---|
51 | |
---|
52 | int status; |
---|
53 | int i; |
---|
54 | int threadparm[THREADS]; |
---|
55 | pthread_t threadid[THREADS]; |
---|
56 | void* thread_stat[THREADS]; |
---|
57 | |
---|
58 | for (i=0; i<THREADS; i++) { |
---|
59 | threadparm[i] = i+1; |
---|
60 | status = pthread_create( &threadid[i], |
---|
61 | NULL, |
---|
62 | threadfunc, |
---|
63 | (void *)&threadparm[i]); |
---|
64 | if ( status < 0) { |
---|
65 | printf("pthread_create failed, errno=%d", errno); |
---|
66 | exit(2); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | for ( i=0; i<THREADS; i++) { |
---|
71 | status = pthread_join( threadid[i], (void **)&thread_stat[i]); |
---|
72 | if ( status < 0) |
---|
73 | printf("pthread_join failed, thread %d, errno=%d\n", i+1, errno); |
---|
74 | |
---|
75 | if (thread_stat[i] != 0) |
---|
76 | printf("bad thread status, thread %d, status=%d\n", i+1, |
---|
77 | (int)thread_stat[i]); |
---|
78 | } |
---|
79 | |
---|
80 | if (once_counter != 1) { |
---|
81 | printf("once_fn did not get control once, counter=%d",once_counter); |
---|
82 | exit(1); |
---|
83 | } |
---|
84 | |
---|
85 | exit(0); |
---|
86 | |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | int main(int argc, char const *argv[]) |
---|
93 | { |
---|
94 | test(); |
---|
95 | return 0; |
---|
96 | } |
---|