]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Thread local in a library. |
2 | */ | |
3 | #include "thr-lib.h" | |
4 | /* | |
5 | * #define NTHREADS 4 | |
6 | * #define NUM_ELEMS 12 | |
7 | */ | |
8 | ||
9 | extern void* adder( void * ); | |
10 | ||
11 | pthread_mutex_t mutex; /* mutex for protecting global data total */ | |
12 | ||
13 | int numbers[NUM_ELEMS] = {5, 4, 3, 2, 1, 6, 7, 8, 9, 10, 12, 11}; | |
14 | int total = 0; | |
15 | ||
16 | int debugger_saw[NTHREADS][ELEMS_PER_THREAD]; /* [4][3] */ | |
17 | int the_code_saw[NTHREADS][ELEMS_PER_THREAD]; | |
18 | ||
19 | int get_number(i) | |
20 | int i; | |
21 | { | |
22 | /* sleep to force context switch to another thread in non-MP system | |
23 | * so that TLS symbols are used by multiple threads concurrently | |
24 | * in some way. | |
25 | */ | |
26 | sleep(1); | |
27 | return numbers[i]; | |
28 | } | |
29 | ||
30 | main() | |
31 | { | |
32 | pthread_t thread[NTHREADS]; | |
33 | void *status; | |
34 | int i, j, ret; | |
35 | ||
36 | printf("== Thread: Test started\n"); | |
37 | ||
38 | for( i = 0; i < NTHREADS; i++ ) { | |
39 | for( j = 0; j < ELEMS_PER_THREAD; j++ ) { | |
40 | debugger_saw[i][j] = 0; | |
41 | the_code_saw[i][j] = 0; | |
42 | } | |
43 | } | |
44 | ||
45 | ret = pthread_mutex_init(&mutex, NULL); | |
46 | if (ret != 0) { | |
47 | printf("== Thread: pthread_mutex_init() error: %d\n", ret); | |
48 | exit(1); | |
49 | } | |
50 | ||
51 | for (i=0; i < NTHREADS; i++) { | |
52 | ret = pthread_create( &thread[i], | |
53 | NULL, | |
54 | adder, | |
55 | (void *) i); | |
56 | if (ret != 0) { | |
57 | printf("== Thread: pthread_create() error: %d\n", ret); | |
58 | exit(1); | |
59 | } | |
60 | printf("== Thread: thread %d created\n", i); | |
61 | } | |
62 | ||
63 | for (i=0; i < NTHREADS; i++) { | |
64 | pthread_join( thread[i], &status); | |
65 | } | |
66 | ||
67 | printf("== Thread: total = %d\n", total); /* Expect "78" */ | |
68 | ||
69 | for( i = 0; i < NTHREADS; i++ ) { | |
70 | for( j = 0; j < ELEMS_PER_THREAD; j++ ) { | |
71 | printf( "== Thread: the debugger saw %d, the program saw %d\n", | |
72 | debugger_saw[i][j], | |
73 | the_code_saw[i][j] ); | |
74 | } | |
75 | } | |
76 | ||
77 | printf("== Thread: Test ended\n"); | |
78 | exit(0); | |
79 | } |