1 /** pthread_spinloc_t test. */
4 /* Make sure pthread_spinlock_t is available when compiling with older glibc
5 * versions (2.3 or before).
10 #include <stdio.h> /* fprintf() */
11 #include <stdlib.h> /* atoi() */
14 static pthread_barrier_t s_barrier
;
15 static pthread_spinlock_t s_spinlock
;
16 static int s_iterations
;
20 static void* thread_func(void* arg
)
24 pthread_barrier_wait(&s_barrier
);
25 for (i
= s_iterations
; i
> 0; i
--)
27 pthread_spin_lock(&s_spinlock
);
29 pthread_spin_unlock(&s_spinlock
);
34 int main(int argc
, char** argv
)
37 const int n_threads
= 10;
38 pthread_t tid
[n_threads
];
40 s_iterations
= argc
> 1 ? atoi(argv
[1]) : 1000;
42 fprintf(stderr
, "Start of test.\n");
43 pthread_barrier_init(&s_barrier
, 0, n_threads
);
44 pthread_spin_init(&s_spinlock
, 0);
45 for (i
= 0; i
< n_threads
; i
++)
46 pthread_create(&tid
[i
], 0, thread_func
, 0);
47 for (i
= 0; i
< n_threads
; i
++)
48 pthread_join(tid
[i
], 0);
49 pthread_spin_destroy(&s_spinlock
);
50 pthread_barrier_destroy(&s_barrier
);
51 if (s_counter
== n_threads
* s_iterations
)
52 fprintf(stderr
, "Test successful.\n");
54 fprintf(stderr
, "Test failed: counter = %d, should be %d\n",
55 s_counter
, n_threads
* s_iterations
);