3 * Tiny test program to see whether POSIX threads work.
6 static const char rcsid[] = "$Id: pthread.c,v 1.4 2005/10/26 22:56:05 chris Exp $";
17 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
18 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
19 static int return_value = -1;
21 void *worker_thread(void *v) {
22 /* Record successful return and signal parent to wake up. */
24 pthread_mutex_lock(&mtx);
25 pthread_cond_signal(&cond);
26 pthread_mutex_unlock(&mtx);
33 /* Start a thread, and have it set a variable to some other value, then signal
34 * a condition variable. If this doesn't happen within some set time, we assume
35 * that something's gone badly wrong and abort (for instance, the thread never
40 struct timespec deadline = {0};
41 if ((res = pthread_mutex_lock(&mtx)) != 0
42 || (res = pthread_create(&thr, NULL, worker_thread, NULL)) != 0) {
43 fprintf(stderr, "%s\n", strerror(res));
47 /* Thread should now be running; we should wait on the condition
50 deadline.tv_sec = 2 + time(NULL);
51 while ((res = pthread_cond_timedwait(&cond, &mtx, &deadline)) == EINTR);
54 fprintf(stderr, "%s\n", strerror(res));
58 if ((res = pthread_cancel(thr)) != 0
59 || (res = pthread_join(thr, NULL)) != 0) {
60 fprintf(stderr, "%s\n", strerror(res));