/*第一次使用windows API線程函數,還望給位大蝦指教啊*/windows
#include <windows.h>
#include <stdio.h>函數
#define BUFFER_SIZE 10線程
HANDLE hSemaphore_Full;
HANDLE hSemaphore_Empty;
HANDLE hMutex;
HANDLE hThread[2];
CHAR lpSharedBuffer[BUFFER_SIZE] = {0};ip
DWORD WINAPI ProducerThread(LPVOID lpParam);
DWORD WINAPI ConsumerThread(LPVOID lpParam);it
void UseMutex(void);
void UseSemaphore(void);
void UseThread(void);io
int main(void)
{
UseSemaphore();
UseMutex();
UseThread();
printf("\n\n");
DWORD i;
WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
{
for (i = 0; i < 2; i++)
{
CloseHandle(hThread[i]);
}
}
printf("\n");
return 0;
}ast
DWORD WINAPI ProduceThread(LPVOID lpParam)
{
DWORD dwWaitResult_Semaphore;
DWORD dwWaitResult_Mutex;
LONG dwPreviousCount;
DWORD j = 0;
for(;j < 20; j++){sed
Sleep(rand()%1000);map
dwWaitResult_Semaphore = WaitForSingleObject(hSemaphore_Empty,
INFINITE
);
switch (dwWaitResult_Semaphore)
{
case WAIT_OBJECT_0:
//#ifdef MUTEX
dwWaitResult_Mutex = WaitForSingleObject(hMutex,
INFINITE);
switch (dwWaitResult_Mutex)
{
case WAIT_OBJECT_0:
//#endif
printf("ProduceProcess %d Gets Semaphore!\n",GetCurrentThreadId());
//#ifdef MUTEX
if (!ReleaseMutex(hMutex))
{
printf("Relase Produce Mutex error : %d\n",GetLastError());
}
else
{
printf("Produce Mutex has been Relased!\n");
}
break;
default:
printf(" Produce Mutex Wait error: %d\n",GetLastError());
}
//#endif
break;
default:
printf("ProduceProcess %u Wait error: %u \n",GetLastError());
}error
Sleep(rand()%1000);
if (!ReleaseSemaphore(hSemaphore_Full,
1,
&dwPreviousCount) )
{
printf("ProduceProcess %u ReleaseSemaphore error: %d \n",
GetCurrentThreadId());
}
else
{
printf("ProduceProcess %u release Semaphore,previous count is %u \n",
GetCurrentThreadId(),
dwPreviousCount);
}
}
return 1;
}
DWORD WINAPI ConsumerThread(LPVOID lpParam)
{
DWORD dwWaitResult_Semaphore;
DWORD dwWaitResult_Mutex;
LONG dwPreviousCount;
DWORD j=0;
for(;j < 20; j++){
Sleep(rand()%1000);
dwWaitResult_Semaphore = WaitForSingleObject(hSemaphore_Full,
INFINITE
);
switch (dwWaitResult_Semaphore)
{
case WAIT_OBJECT_0:
//#ifdef MUTEX
dwWaitResult_Mutex = WaitForSingleObject(hMutex,
INFINITE);
switch (dwWaitResult_Mutex)
{
case WAIT_OBJECT_0:
//#endif
//訪問臨界區
printf("ConsumerProcess %d Gets Semaphore!\n",GetCurrentThreadId());
//#ifdef MUTEX
if (!ReleaseMutex(hMutex))
{
printf("Consumer Relase Mutex error : %d\n",GetLastError());
}
else
{
printf("Consumer Mutex has been Relased!\n");
}
break;
default:
printf("Consumer mutex Wait error: %d\n",GetLastError());
}
//#endif
break;
default:
printf("ConsumerProcess %u Wait error: %u \n",GetLastError());
}
Sleep(rand()%1000);
if (!ReleaseSemaphore(hSemaphore_Empty,
1,
&dwPreviousCount) )
{
printf("ConsumerProcess %u ReleaseSemaphore error: %d \n",
GetCurrentThreadId());
}
else
{
printf("ConsumerProcess %u release Semaphore,previous count is %u \n",
GetCurrentThreadId(),
dwPreviousCount);
}
}
return 1;
}
void UseMutex(void)
{
//#ifdef MUTEX
hMutex = CreateMutex(NULL,
FALSE,
NULL);
if (hMutex == NULL)
{
printf("CreateMutex error: %d\n",GetLastError());
ExitProcess(0);
}
else
{
printf("Mutex has been Created!\n");
}
//#endif
}
void UseSemaphore(void)
{
hSemaphore_Full = CreateSemaphore(NULL,
0,//初始化計數器
BUFFER_SIZE,//最大計數
NULL//名字
);
if (hSemaphore_Full == NULL)
{
printf("CreateSemaphore_Full error: %d\n",GetLastError());
ExitProcess(0);
}
else
{
printf("Semaphore_Full has been Created!\n");
}
hSemaphore_Empty = CreateSemaphore(NULL,
BUFFER_SIZE,//初始化計數器
BUFFER_SIZE,//最大計數
NULL//名字
);
if (hSemaphore_Empty == NULL)
{
printf("CreateSemaphore_Empty error: %d\n",GetLastError());
ExitProcess(0);
}
else
{
printf("Semaphore_Empty has been Created!\n");
}
}
void UseThread(void) { hThread[0] = CreateThread(NULL, 0, ProduceThread, NULL, 0, NULL); if (hThread[0] == NULL) { printf("Create ProduceThread error! (%d)\n",GetLastError()); ExitProcess(0); } else { printf("ProduceThread has been Created!\n"); } hThread[1] = CreateThread(NULL, 0, ConsumerThread, NULL, 0, NULL); if (hThread[1] == NULL) { printf("Create ConsumerThread error! (%d)\n",GetLastError()); ExitProcess(0); } else { printf("ConsumerThread has been Created!\n"); } }