source: tests/concurrent/pthread/pthread_once_test.cfa @ 20be782

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