00001 /* 00002 Thread example (cancellation) 00003 AUP2, Sec. 5.17.5 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include <pthread.h> 00023 00024 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; 00025 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 00026 00027 struct node { 00028 int n_number; 00029 struct node *n_next; 00030 } *head = NULL; 00031 00032 /*[thread_func]*/ 00033 static void *thread_func(void *arg) 00034 { 00035 struct node *p; 00036 00037 while (true) { 00038 ec_rv( pthread_mutex_lock(&mtx) ) 00039 while (head == NULL) 00040 ec_rv( pthread_cond_wait(&cond, &mtx) ) 00041 p = head; 00042 head = head->n_next; 00043 printf("Got %d from front of queue\n", p->n_number); 00044 free(p); 00045 ec_rv( pthread_mutex_unlock(&mtx) ) 00046 } 00047 return (void *)true; 00048 00049 EC_CLEANUP_BGN 00050 (void)pthread_mutex_unlock(&mtx); 00051 EC_FLUSH("thread_func") 00052 return (void *)false; 00053 EC_CLEANUP_END 00054 } 00055 /*[]*/ 00056 00057 int main(void) 00058 { 00059 pthread_t tid; 00060 int i; 00061 struct node *p; 00062 00063 ec_rv( pthread_create(&tid, NULL, thread_func, NULL) ) 00064 /*[tx6-main]*/ 00065 for (i = 0; i < 10; i++) { 00066 ec_null( p = malloc(sizeof(struct node)) ) 00067 p->n_number = i; 00068 ec_rv( pthread_mutex_lock(&mtx) ) 00069 p->n_next = head; 00070 head = p; 00071 ec_rv( pthread_cond_signal(&cond) ) 00072 ec_rv( pthread_mutex_unlock(&mtx) ) 00073 sleep(1); 00074 } 00075 ec_rv( pthread_cancel(tid) ) 00076 ec_rv( pthread_join(tid, NULL) ) 00077 printf("All done -- exiting\n"); 00078 return EXIT_SUCCESS; 00079 /*[]*/ 00080 00081 EC_CLEANUP_BGN 00082 return EXIT_FAILURE; 00083 EC_CLEANUP_END 00084 }