Annotation of embedaddon/iftop/config/pthread.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * pthread.c:
                      3:  * Tiny test program to see whether POSIX threads work.
                      4:  */
                      5: 
                      6: static const char rcsid[] = "$Id: pthread.c,v 1.4 2005/10/26 22:56:05 chris Exp $";
                      7: 
                      8: #include <sys/types.h>
                      9: 
                     10: #include <errno.h>
                     11: #include <pthread.h>
                     12: #include <stdio.h>
                     13: #include <string.h>
                     14: #include <time.h>
                     15: #include <unistd.h>
                     16: 
                     17: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
                     18: static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
                     19: static int return_value = -1;
                     20: 
                     21: void *worker_thread(void *v) {
                     22:     /* Record successful return and signal parent to wake up. */
                     23:     return_value = 0;
                     24:     pthread_mutex_lock(&mtx);
                     25:     pthread_cond_signal(&cond);
                     26:     pthread_mutex_unlock(&mtx);
                     27:     while (1) {
                     28:         sleep(1);
                     29:         pthread_testcancel();
                     30:     }
                     31: }
                     32: 
                     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
                     36:  * got started). */
                     37: int main(void) {
                     38:     pthread_t thr;
                     39:     int res;
                     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));
                     44:         return -1;
                     45:     }
                     46: 
                     47:     /* Thread should now be running; we should wait on the condition
                     48:      * variable. */
                     49:     do
                     50:         deadline.tv_sec = 2 + time(NULL);
                     51:     while ((res = pthread_cond_timedwait(&cond, &mtx, &deadline)) == EINTR);
                     52:     
                     53:     if (res != 0) {
                     54:         fprintf(stderr, "%s\n", strerror(res));
                     55:         return -1;
                     56:     }
                     57: 
                     58:     if ((res = pthread_cancel(thr)) != 0
                     59:         || (res = pthread_join(thr, NULL)) != 0) {
                     60:         fprintf(stderr, "%s\n", strerror(res));
                     61:         return -1;
                     62:     }
                     63:     
                     64:     return return_value;
                     65: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>