I. Problem Description Producer - Consumer Problem is a classic process synchronization problem, which is first proposed by Dijkstra to demonstrate his proposed semaphore. This job requires two threads executed in the same process address space. Producer threads produces items, then place the item in an air buffer for consumer thread consumption. The consumer thread obtains items from the buffer and then releases the buffer. When the producer thread produces items, if there is no air buffer available, the producer thread must wait for the consumer thread to release an air buffer. When consumer thread consumement items, if there is no full buffer, the consumer thread will be blocked until the new item is produced.
Second, implementation code 0000 #include 0038 0039 Handle Hthreads [THREADS_COUNT]; // Handle0040 DWORD PRODUCERID [Products_count]; // Producer Thread Identifier 0041 DWORD Consumerid [consuMERS_COUNT]; // Consumer thread identifier 0042 0043 // Create a producer Thread 0044 for (int i = 0; i Simply simulate, only the ID number of the new product is output 0066 void products () 0067 {0068 st: cout << "products" << products << "..."; 0069 std :: cout << " Succeed << std :: endl; 0070} 0071 0072 // put the new product into buffer 0073 void append () 0074 {0075 std :: cout << "appending a product ..."; 0076 g_buffer [ IN] = productID; 0077 IN = (in 1)% size_of_buffer; 0078 std :: couk << "succeed << std :: endl; 0079 0080 // Output buffer current status 0081 for (int i = 0 i