多线程

程序描述:

主线程启动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]);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值