#include <windows.h>
#include <stdio.h>
#define MAXPRODUCTCNT 100 // Max product Count
#define BUFSIZE 4 // product buffer size
HANDLE g_hThreads[2]; //Thread handles, Produce thread and consume thread
HANDLE g_hNotFullEvent = NULL; // Product buffer is not full event
HANDLE g_hNotEmptyEvent = NULL; // product buffer is not empty event
HANDLE g_hMutex = NULL; // Mutex
int g_iStorageCnt = 0; // product count in buffer
int g_iProductIdx = 0; // Index of the last produced product
int g_iConsumeIdx = 0; // Index of the last consumed product
DWORD WINAPI ProduceThreadProc(LPVOID lpParam)
{
DWORD dwWaitResult = 0;
// two objects.
// Mutex and Not full event.
HANDLE hObjects[2] = {NULL, NULL};
hObjects[0] = g_hMutex;
hObjects[1] = g_hNotFullEvent;
for (int iIdx = 0; iIdx < MAXPRODUCTCNT; iIdx++)
{
//Wait till Mutex and Not full event are set signaled.
dwWaitResult = WaitForMultipleObjects(
2, // number of handles in array
hObjects, // array of handles
TRUE, // wait till all are signaled
INFINITE); // indefinite wait
if (WAIT_OBJECT_0 != dwWaitResult)
{
printf("Wait error: (%d)\n", GetLastError());
ExitThread(0);
}
// Create a Product
g_iProductIdx++;
g_iStorageCnt++;
printf("Create Product %d\n", g_iProductIdx);
// check whether product buffer is full
if (g_iStorageCnt >= BUFSIZE)
{
if (!ResetEvent(g_hNotFullEvent) )
{
printf("SetEvent failed (%d)\n", GetLastError());
ExitThread(0);
}
}
// set not empty event signaled.
if (!SetEvent(g_hNotEmptyEvent) )
{
printf("SetEvent failed (%d)\n", GetLastError());
ExitThread(0);
}
// Release mutex
ReleaseMutex(g_hMutex);
}
return 1;
}
DWORD WINAPI ConsumeThreadProc(LPVOID lpParam)
{
DWORD dwWaitResult = 0;
// two objects.
// Mutex and Not empty event.
HANDLE hObjects[2] = {NULL, NULL};
hObjects[0] = g_hMutex;
hObjects[1] = g_hNotEmptyEvent;
for (int iIdx = 0; iIdx < MAXPRODUCTCNT; iIdx++)
{
//Wait till Mutex and Not empty event are set signaled.
dwWaitResult = WaitForMultipleObjects(
2, // number of handles in array
hObjects, // array of handles
TRUE, // wait till all are signaled
INFINITE); // indefinite wait
if (WAIT_OBJECT_0 != dwWaitResult)
{
printf("Wait error: (%d)\n", GetLastError());
ExitThread(0);
}
// Consume a Product
g_iStorageCnt--;
g_iConsumeIdx++;
printf("Consume product %d(%d)\n", g_iConsumeIdx, g_iProductIdx);
// check whether product buffer is empty
if (g_iStorageCnt < 1)
{
if (!ResetEvent(g_hNotEmptyEvent) )
{
printf("SetEvent failed (%d)\n", GetLastError());
ExitThread(0);
}
}
/*set not full event signaled*/
if (!SetEvent(g_hNotFullEvent) )
{
printf("SetEvent failed (%d)\n", GetLastError());
ExitThread(0);
}
//Release mutex
ReleaseMutex(g_hMutex);
}
return 1;
}
void main()
{
DWORD dwThreadID[2] = {0, 0};
// Create a manual-reset event object.
// set this to signaled when the product buffer is not empty.
g_hNotEmptyEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // initial state is nonsignaled
NULL // unnamed Event
);
if (NULL == g_hNotEmptyEvent)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return;
}
// Create a manual-reset event object.
// set this to signaled when the product buffer is not full.
g_hNotFullEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
TRUE, // initial state is signaled
NULL // unnamed Event
);
if (NULL == g_hNotFullEvent)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return;
}
// Create a mutex with no initial owner
g_hMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (NULL == g_hMutex)
{
printf("CreateMutex error: (%d)\n", GetLastError());
return;
}
// Create a thread to produce products.
g_hThreads[0] = CreateThread(NULL,
0,
ProduceThreadProc,
NULL,
0,
&dwThreadID[0]);
if (NULL == g_hThreads[0])
{
printf("CreateThread failed (%d)\n", GetLastError());
return;
}
// Create a thred to consume products.
g_hThreads[1] = CreateThread(NULL,
0,
ConsumeThreadProc,
NULL,
0,
&dwThreadID[1]);
if (g_hThreads[1] == NULL)
{
printf("CreateThread failed (%d)\n", GetLastError());
return;
}
DWORD dwWaitResult = WaitForMultipleObjects(
2, // number of handles in array
g_hThreads, // array of thread handles
TRUE, // wait till all threads quit
INFINITE); // indefinite wait
if (WAIT_OBJECT_0 != dwWaitResult)
{
printf("Wait error : (%d)\n", GetLastError());
return;
}
}
//------------------------------------火车票模拟
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
HANDLE hThread1,hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
hMutex=CreateMutex(NULL,FALSE,NULL);
//TRUE代表主线程拥有互斥对象 但是主线程没有释放该对象 互斥对象谁拥有 谁释放
//FLASE代表当前没有线程拥有这个互斥对象
Sleep(4000);
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while (true)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
cout<<"t1: "<<tickets--<<endl;
}
else
{
break;
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while (true)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
cout<<"t2: "<<tickets--<<endl;
}
else
{
break;
}
ReleaseMutex(hMutex);
}
return 0;
}