程序描述:
主线程启动10个子线程并将表示子线程序号的变量地址作为参数传递给子线程。子线程接收参数 -> sleep(50) -> 全局变量加1 -> sleep(0) -> 输出参数和全局变量。
要求:
1.子线程输出的线程序号不能重复。
2.全局变量的输出必须递增。
方法一:Event+CriticalSection
#include <process.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int gCount;
HANDLE hEvent;
CRITICAL_SECTION cs;
unsigned __stdcall fun(void* p)
{
int i = *(int*)p;
SetEvent(hEvent);
Sleep(50);
EnterCriticalSection(&cs);
gCount++;
Sleep(0);
//cout << "ThreadID: " << i << " " << gCount << endl;
printf("ThreadID: %d\t Global data: %d\n", i, gCount);
LeaveCriticalSection(&cs);
return 0;
}
void main()
{
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
InitializeCriticalSection(&cs);
const int thdNum = 10;
HANDLE handle[thdNum];
for (int i = 0; i < thdNum; i++)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, fun, &i, 0, NULL);
WaitForSingleObject(hEvent, INFINITE);
}
WaitForMultipleObjects(thdNum, handle, TRUE, INFINITE);
//清理资源对象
CloseHandle(hEvent);
DeleteCriticalSection(&cs);
for (int i = 0; i < thdNum; i++)
{
CloseHandle(handle[i]);
}
}
方法二:Event+Mutex
#include <process.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int gCount;
HANDLE hEvent;
HANDLE hMutex;
unsigned __stdcall fun(void* p)
{
int i = *(int*)p;
SetEvent(hEvent);
Sleep(50);
WaitForSingleObject(hMutex, INFINITE);
gCount++;
Sleep(0);
//cout << "ThreadID: " << i << " " << gCount << endl;
printf("ThreadID: %d\t Global data: %d\n", i, gCount);
ReleaseMutex(hMutex);
return 0;
}
void main()
{
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hMutex = CreateMutex(NULL, FALSE, NULL);
const int thdNum = 10;
HANDLE handle[thdNum];
for (int i = 0; i < thdNum; i++)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, fun, &i, 0, NULL);
WaitForSingleObject(hEvent, INFINITE);
}
WaitForMultipleObjects(thdNum, handle, TRUE, INFINITE);
//清理资源对象
CloseHandle(hEvent);
CloseHandle(hMutex);
for (int i = 0; i < thdNum; i++)
{
CloseHandle(handle[i]);
}
}
方法三:Semaphore+Mutex
#include <process.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int gCount;
HANDLE hSemaphore;
HANDLE hMutex;
unsigned __stdcall fun(void* p)
{
int i = *(int*)p;
ReleaseSemaphore(hSemaphore, 1, NULL);
Sleep(50);
WaitForSingleObject(hMutex, INFINITE);
gCount++;
Sleep(0);
//cout << "ThreadID: " << i << " " << gCount << endl;
printf("ThreadID: %d\t Global data: %d\n", i, gCount);
ReleaseMutex(hMutex);
return 0;
}
void main()
{
hSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
hMutex = CreateMutex(NULL, FALSE, NULL);
const int thdNum = 10;
HANDLE handle[thdNum];
for (int i = 0; i < thdNum; i++)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, fun, &i, 0, NULL);
WaitForSingleObject(hSemaphore, INFINITE);
}
WaitForMultipleObjects(thdNum, handle, TRUE, INFINITE);
//清理资源对象
CloseHandle(hSemaphore);
CloseHandle(hMutex);
for (int i = 0; i < thdNum; i++)
{
CloseHandle(handle[i]);
}
}
方法四:Semaphore+CriticalSection
#include <process.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int gCount;
HANDLE hSemaphore;
CRITICAL_SECTION cs;
unsigned __stdcall fun(void* p)
{
int i = *(int*)p;
ReleaseSemaphore(hSemaphore, 1, NULL);
Sleep(50);
EnterCriticalSection(&cs);
gCount++;
Sleep(0);
//cout << "ThreadID: " << i << " " << gCount << endl;
printf("ThreadID: %d\t Global data: %d\n", i, gCount);
LeaveCriticalSection(&cs);
return 0;
}
void main()
{
hSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
InitializeCriticalSection(&cs);
const int thdNum = 10;
HANDLE handle[thdNum];
for (int i = 0; i < thdNum; i++)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, fun, &i, 0, NULL);
WaitForSingleObject(hSemaphore, INFINITE);
}
WaitForMultipleObjects(thdNum, handle, TRUE, INFINITE);
//清理资源对象
CloseHandle(hSemaphore);
DeleteCriticalSection(&cs);
for (int i = 0; i < thdNum; i++)
{
CloseHandle(handle[i]);
}
}