©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c5/tx5.c

Go to the documentation of this file.
00001 /*
00002     Thread example (condition variable)
00003     AUP2, Sec. 5.17.4
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 /*[tx5]*/
00025 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
00026 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00027 
00028 struct node {
00029     int n_number;
00030     struct node *n_next;
00031 } *head = NULL;
00032 
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 int main(void)
00057 {
00058     pthread_t tid;
00059     int i;
00060     struct node *p;
00061 
00062     ec_rv( pthread_create(&tid, NULL, thread_func, NULL) )
00063     for (i = 0; i < 10; i++) {
00064         ec_null( p = malloc(sizeof(struct node)) )
00065         p->n_number = i;
00066         ec_rv( pthread_mutex_lock(&mtx) )
00067         p->n_next = head;
00068         head = p;
00069         ec_rv( pthread_cond_signal(&cond) )
00070         ec_rv( pthread_mutex_unlock(&mtx) )
00071         sleep(1);
00072     }
00073     ec_rv( pthread_join(tid, NULL) )
00074     printf("All done -- exiting\n");
00075     return EXIT_SUCCESS;
00076 
00077 EC_CLEANUP_BGN
00078     return EXIT_FAILURE;
00079 EC_CLEANUP_END
00080 }
00081 /*[]*/

Generated on Fri Apr 23 10:56:57 2004 for AUP2 Example Source by doxygen 1.3.1