1 /* Thread local in a library.
9 extern void* adder( void * );
11 pthread_mutex_t mutex
; /* mutex for protecting global data total */
13 int numbers
[NUM_ELEMS
] = {5, 4, 3, 2, 1, 6, 7, 8, 9, 10, 12, 11};
16 int debugger_saw
[NTHREADS
][ELEMS_PER_THREAD
]; /* [4][3] */
17 int the_code_saw
[NTHREADS
][ELEMS_PER_THREAD
];
22 /* sleep to force context switch to another thread in non-MP system
23 * so that TLS symbols are used by multiple threads concurrently
32 pthread_t thread
[NTHREADS
];
36 printf("== Thread: Test started\n");
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;
45 ret
= pthread_mutex_init(&mutex
, NULL
);
47 printf("== Thread: pthread_mutex_init() error: %d\n", ret
);
51 for (i
=0; i
< NTHREADS
; i
++) {
52 ret
= pthread_create( &thread
[i
],
57 printf("== Thread: pthread_create() error: %d\n", ret
);
60 printf("== Thread: thread %d created\n", i
);
63 for (i
=0; i
< NTHREADS
; i
++) {
64 pthread_join( thread
[i
], &status
);
67 printf("== Thread: total = %d\n", total
); /* Expect "78" */
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",
77 printf("== Thread: Test ended\n");
This page took 0.029955 seconds and 4 git commands to generate.