简单的生产者消费者问题,多线程达不到理想效果
程序代码:
// BoundedBuffer.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include "process.h" #define MAXLEN 10 int BUFFER = 0; HANDLE hMutex; HANDLE hSemaphore; CRITICAL_SECTION cs; void MyMutex(); void MyCriticalSection(); void MyThread(); int _tmain(int argc, _TCHAR* argv[]) { // MyMutex(); // MyCriticalSection(); MyThread(); system("pause"); return 0; } // 下面两线程用互斥量实现 DWORD WINAPI producer(LPVOID *a) { while(1) { WaitForSingleObject(hMutex,INFINITE); if(BUFFER >=10) { printf("篮子已满****\n"); Sleep(1000); } else { BUFFER++; printf("生产一个产品\n"); Sleep(1000); } printf("此时的产品为: %d\n",BUFFER); Sleep(1000); ReleaseMutex(hMutex); } } DWORD WINAPI consumer(LPVOID *a) { while(1) { WaitForSingleObject(hMutex,INFINITE); if(BUFFER <=0) { printf("篮子已空&&&&&\n"); Sleep(200); } else { BUFFER--; printf("消费一个产品\n"); Sleep(200); } printf("此时的产品为: %d\n",BUFFER); Sleep(1000); ReleaseMutex(hMutex); } } //下面两线程用临界区实现 DWORD WINAPI producer1(LPVOID *a) { while(1) { EnterCriticalSection(&cs); if(BUFFER >=10) { printf("篮子已满****\n"); Sleep(1000); } else { BUFFER++; printf("生产一个产品\n"); Sleep(1000); } printf("此时的产品为: %d\n",BUFFER); Sleep(1000); LeaveCriticalSection(&cs); } } DWORD WINAPI consumer1(LPVOID *a) { while(1) { EnterCriticalSection(&cs); if(BUFFER <=0) { printf("篮子已空&&&&&\n"); Sleep(1000); } else { BUFFER--; printf("消费一个产品\n"); Sleep(1000); } printf("此时的产品为: %d\n",BUFFER); Sleep(1000); LeaveCriticalSection(&cs); } } //下面两线程用信号量实现 DWORD WINAPI producer2(LPVOID *a) { while(1) { WaitForSingleObject(hSemaphore,INFINITE); if(BUFFER >=10) { printf("篮子已满****\n"); Sleep(1000); } else { BUFFER++; printf("生产一个产品\n"); Sleep(1000); } printf("此时的产品为: %d\n",BUFFER); Sleep(1000); ReleaseSemaphore(hSemaphore,1,NULL); } return 23; } DWORD WINAPI consumer2(LPVOID *a) { while(1) { WaitForSingleObject(hSemaphore,INFINITE); if(BUFFER <=0) { printf("篮子已空&&&&&\n"); Sleep(1000); } else { BUFFER--; printf("消费一个产品\n"); Sleep(1000); } printf("此时的产品为: %d\n",BUFFER); Sleep(1000); ReleaseSemaphore(hSemaphore,1,NULL); } return 22; } //互斥量 void MyMutex() { hMutex = CreateMutex(NULL,FALSE,NULL); HANDLE handle1,handle2; DWORD dw1,dw2; handle1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)producer,NULL,0,&dw1); handle2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer,&dw1,0,&dw2); while(1); } //临界区 void MyCriticalSection() { InitializeCriticalSection(&cs); HANDLE handle3,handle4; DWORD dw3,dw4; handle3 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)producer1,NULL,0,&dw3); handle4 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer1,NULL,0,&dw4); while(1); } // 信号量 void MyThread() { hSemaphore = CreateSemaphore(NULL,1,3,NULL); HANDLE handle5,handle6; DWORD dw5,dw6; handle5 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)producer2,NULL,0,&dw5); Sleep(1000); handle6 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer2,NULL,0,&dw6); Sleep(1000); while(1); }MyMutex() 实现效果 生产,消费,生产,消费,生产,消费……
MyCriticalSection() 实现效果 生产,生产,生产->满,消费,消费,消费 -> 空
MyThread() 实现效果 与MyMutex() 一样 生产,消费,生产,消费……
问:为什么不是随机的生产消费,如生产,生产,消费,…………
消费,生产,生产,生产,消费……
能不能详细解释下上面3个实现方式为什么是有规律的?以及如何改正