标签:
1 #include <windows.h> 2 #include <iostream> 3 const unsigned short SIZE_OF_BUFFER = 10; //缓冲区长度 4 unsigned short ProductID = 0; //产品号 5 unsigned short ConsumeID = 0; //将被消耗的产品号 6 unsigned short in = 0; //产品进缓冲区时的缓冲区下标 7 unsigned short out = 0; //产品出缓冲区时的缓冲区下标 8 int g_buffer[SIZE_OF_BUFFER]; //缓冲区是个循环队列 9 bool g_continue = true; //控制程序结束 10 HANDLE g_hMutex; //用于线程间的互斥 11 HANDLE g_hFullSemaphore; //当缓冲区满时迫使生产者等待 12 HANDLE g_hEmptySemaphore; //当缓冲区空时迫使消费者等待 13 DWORD WINAPI Producer(LPVOID); //生产者线程 14 DWORD WINAPI Consumer(LPVOID); //消费者线程 15 int main() 16 { 17 int i; 18 //创建各个互斥信号 19 g_hMutex = CreateMutex(NULL,FALSE,NULL); 20 g_hFullSemaphore = CreateSemaphore(NULL,SIZE_OF_BUFFER-1,SIZE_OF_BUFFER-1,NULL); 21 g_hEmptySemaphore = CreateSemaphore(NULL,0,SIZE_OF_BUFFER-1,NULL); 22 //调整下面的数值,可以发现,当生产者个数多于消费者个数时, 23 //生产速度快,生产者经常等待消费者;反之,消费者经常等待 24 const unsigned short PRODUCERS_COUNT = 3; //生产者的个数 25 const unsigned short CONSUMERS_COUNT = 1; //消费者的个数 26 //总的线程数 27 const unsigned short THREADS_COUNT = PRODUCERS_COUNT+CONSUMERS_COUNT; 28 HANDLE hThreads[PRODUCERS_COUNT]; //各线程的handle 29 DWORD producerID[CONSUMERS_COUNT]; //生产者线程的标识符 30 DWORD consumerID[THREADS_COUNT]; //消费者线程的标识符 31 //创建生产者线程 32 for (i=0;i<PRODUCERS_COUNT;++i){ 33 hThreads[i]=CreateThread(NULL,0,Producer,NULL,0,&producerID[i]); 34 if (hThreads[i]==NULL) return -1; 35 } 36 //创建消费者线程 37 for (i=0;i<CONSUMERS_COUNT;++i){ 38 hThreads[PRODUCERS_COUNT+i]=CreateThread(NULL,0,Consumer,NULL,0,&consumerID[i]); 39 if (hThreads[i]==NULL) return -1; 40 } 41 while(g_continue){ 42 if(getchar()){ //按回车后终止程序运行 43 g_continue = false; 44 } 45 } 46 return 0; 47 } 48 //生产一个产品。简单模拟了一下,仅输出新产品的ID号 49 void Produce() 50 { 51 std::cerr << "Producing " << ++ProductID << " ... "; 52 std::cerr << "Succeed" << std::endl; 53 } 54 //把新生产的产品放入缓冲区 55 void Append() 56 { 57 std::cerr << "Appending a product ... "; 58 g_buffer[in] = ProductID; 59 in = (in+1)%SIZE_OF_BUFFER; 60 std::cerr << "Succeed" << std::endl; 61 //输出缓冲区当前的状态 62 for (int i=0;i<SIZE_OF_BUFFER;++i){ 63 std::cout << i <<": " << g_buffer[i]; 64 if (i==in) std::cout << " <-- 生产"; 65 if (i==out) std::cout << " <-- 消费"; 66 std::cout << std::endl; 67 } 68 } 69 //从缓冲区中取出一个产品 70 void Take() 71 { 72 std::cerr << "Taking a product ... "; 73 ConsumeID = g_buffer[out]; 74 out = (out+1)%SIZE_OF_BUFFER; 75 std::cerr << "Succeed" << std::endl; 76 //输出缓冲区当前的状态 77 for (int i=0;i<SIZE_OF_BUFFER;++i){ 78 std::cout << i <<": " << g_buffer[i]; 79 if (i==in) std::cout << " <-- 生产"; 80 if (i==out) std::cout << " <-- 消费"; 81 std::cout << std::endl; 82 } 83 } 84 //消耗一个产品 85 void Consume() 86 { 87 std::cerr << "Consuming " << ConsumeID << " ... "; 88 std::cerr << "Succeed" << std::endl; 89 } 90 //生产者 91 DWORD WINAPI Producer(LPVOID lpPara) 92 { 93 while(g_continue){ 94 WaitForSingleObject(g_hFullSemaphore,INFINITE); 95 WaitForSingleObject(g_hMutex,INFINITE); 96 Produce(); 97 Append(); 98 Sleep(1500); 99 ReleaseMutex(g_hMutex); 100 ReleaseSemaphore(g_hEmptySemaphore,1,NULL); 101 } 102 return 0; 103 } 104 //消费者 105 DWORD WINAPI Consumer(LPVOID lpPara) 106 { 107 while(g_continue){ 108 WaitForSingleObject(g_hEmptySemaphore,INFINITE); 109 WaitForSingleObject(g_hMutex,INFINITE); 110 Take(); 111 Consume(); 112 Sleep(1500); 113 ReleaseMutex(g_hMutex); 114 ReleaseSemaphore(g_hFullSemaphore,1,NULL); 115 } 116 return 0; 117 }
标签:
原文地址:http://www.cnblogs.com/auto1945837845/p/5385050.html