标签:exit ima create col image 共享 模型 调用 需要
生产者消费者模型如下:
程序如下:
1 #include <unistd.h> 2 #include <sys/types.h> 3 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <errno.h> 7 #include <string.h> 8 9 #include <pthread.h> 10 11 int g_Count = 0; 12 13 int nNum, nLoop; 14 15 //定义锁 16 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 17 18 //定义条件并初始化 19 pthread_cond_t my_condition=PTHREAD_COND_INITIALIZER; 20 21 #define CUSTOM_COUNT 2 22 #define PRODUCT_COUNT 4 23 24 25 // int pthread_mutex_lock(pthread_mutex_t *mutex); 26 // int pthread_mutex_trylock(pthread_mutex_t *mutex); 27 // int pthread_mutex_unlock(pthread_mutex_t *mutex); 28 29 /* 30 int pthread_cond_timedwait(pthread_cond_t *restrict cond, 31 pthread_mutex_t *restrict mutex, 32 const struct timespec *restrict abstime); 33 int pthread_cond_wait(pthread_cond_t *restrict cond, 34 pthread_mutex_t *restrict mutex); 35 */ 36 37 //posix 线程库的函数 线程库 38 void *consume(void* arg) 39 { 40 41 int inum = 0; 42 inum = (int)arg; 43 while(1) 44 { 45 pthread_mutex_lock(&mutex); 46 printf("consum:%d\n", inum); 47 while (g_Count == 0) //while 醒来以后需要重新判断 条件g_Count是否满足,如果不满足,再次wait 48 { 49 printf("consum:%d 开始等待\n", inum); 50 pthread_cond_wait(&my_condition, &mutex); //api做了三件事情 //pthread_cond_wait假醒 51 printf("consum:%d 醒来\n", inum); 52 } 53 54 printf("consum:%d 消费产品begin\n", inum); 55 g_Count--; //消费产品 56 printf("consum:%d 消费产品end\n", inum); 57 58 pthread_mutex_unlock(&mutex); 59 60 sleep(1); 61 } 62 63 pthread_exit(0); 64 65 } 66 67 //生产者线程 68 // 69 void *produce(void* arg) 70 { 71 int inum = 0; 72 inum = (int)arg; 73 74 while(1) 75 { 76 77 /* 78 //因为是很多生产者调用produce,要保护全局变量g_Count,所以加锁 79 pthread_mutex_lock(&mutex); 80 if (g_Count > 20) 81 { 82 printf("produce:%d 产品太多,需要控制,休眠\n", inum); 83 pthread_mutex_unlock(&mutex); 84 sleep(1); 85 continue; 86 } 87 else 88 { 89 pthread_mutex_unlock(&mutex); 90 } 91 */ 92 93 pthread_mutex_lock(&mutex); 94 printf("产品数量:%d\n", g_Count); 95 printf("produce:%d 生产产品begin\n", inum); 96 g_Count++; 97 //只要我生产出一个产品,就告诉消费者去消费 98 printf("produce:%d 生产产品end\n", inum); 99 100 printf("produce:%d 发条件signal begin\n", inum); 101 pthread_cond_signal(&my_condition); //通知,在条件上等待的线程 102 printf("produce:%d 发条件signal end\n", inum); 103 104 pthread_mutex_unlock(&mutex); 105 sleep(1); 106 } 107 108 pthread_exit(0); 109 110 } 111 112 //结论:return arg 和 pthread_exit()的结果都可以让pthread_join 接过来 113 int main() 114 { 115 int i =0; 116 pthread_t tidArray[CUSTOM_COUNT+PRODUCT_COUNT+10]; 117 118 //创建消费者线程 119 for (i=0; i<CUSTOM_COUNT; i++) 120 { 121 pthread_create(&tidArray[i], NULL, consume, (void *)i); 122 } 123 124 sleep(1); 125 //创建生产线程 126 for (i=0; i<PRODUCT_COUNT; i++) 127 { 128 pthread_create(&tidArray[i+CUSTOM_COUNT], NULL, produce, (void*)i); 129 } 130 131 132 133 for (i=0; i<CUSTOM_COUNT+PRODUCT_COUNT; i++) 134 { 135 pthread_join(tidArray[i], NULL); //等待线程结束。。。 136 } 137 138 139 printf("进程也要结束1233\n"); 140 141 return 0; 142 }
执行结果如下:
13.1 多线程操作共享内存、生产者消费者模型、多线程服务器框架
标签:exit ima create col image 共享 模型 调用 需要
原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9457764.html