[置顶] Windows编程之线程同步


Windows编程中线程同步的主要机制:互斥、事件、信号量、可等待定时器,不说了,直接上代码:

// ThreadSync.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>

#define TEST_EVENT_NAME TEXT("TEST_EVENT")
#define TEST_MUTEX_NAME TEXT("TEST_MUTEX")
#define TEST_SEMAPHORE_NAME TEXT("TEST_SEMAPHORE")
#define TEST_WATIABLETIME_NAME TEXT("TEST_TIMER")

void myprintf(char* p)
{
	printf("ThreadId: %d, %s\n", GetCurrentThreadId(), p);
}


HANDLE CreateSyncObject(int flag)
{
	HANDLE hObj = NULL;
	switch (flag)
	{
	case 1:
		myprintf("Use Event!");
		hObj = CreateEvent(NULL, TRUE, FALSE, TEST_EVENT_NAME);
		break;
	case 2:
		myprintf("Use Mutex!");
		hObj = CreateMutex(NULL, FALSE, TEST_MUTEX_NAME);
		break;
	case 3:
		myprintf("Use Semaphore!");
		hObj = CreateSemaphore(NULL, 0, 2, TEST_SEMAPHORE_NAME);
		break;
	case 4:
		myprintf("Use WatiableTimer!");
		hObj = CreateWaitableTimer(NULL, FALSE, TEST_WATIABLETIME_NAME);
		break;
	default:
		break;
	}
	return hObj;
}

HANDLE OpenSyncObject(int flag)
{
	HANDLE hObj = NULL;
	switch (flag)
	{
	case 1:
		myprintf("Open Event!");
		hObj = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEST_EVENT_NAME);
		break;
	case 2:
		myprintf("Open Mutex!");
		hObj = OpenMutex(MUTEX_ALL_ACCESS, FALSE, TEST_MUTEX_NAME);
		break;
	case 3:
		myprintf("Open Semaphore!");
		hObj = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, TEST_SEMAPHORE_NAME);
		break;
	case 4:
		myprintf("Open WatiableTimer!");
		hObj = CreateWaitableTimer(NULL, FALSE, TEST_WATIABLETIME_NAME);
		break;
	default:
		break;
	}
	return hObj;
}

DWORD __stdcall ThrdFun1(LPVOID p)
{
	HANDLE hObj = OpenSyncObject((int)p);
	DWORD dwFlag;
	LONG lCount = 0;
	switch ((int)p)
	{
	case 1:
		dwFlag = WaitForSingleObject(hObj, INFINITE);
		myprintf("Thrd1 Wait Success!");
		ResetEvent(hObj);
		break;
	case 2:
		dwFlag = WaitForSingleObject(hObj, INFINITE);
		myprintf("Thrd1 Wait Success!");
		Sleep(2000);
		ReleaseMutex(hObj);
		break;
	case 3:
		dwFlag = WaitForSingleObject(hObj, INFINITE);
		myprintf("Thrd1 Wait Success!");
		Sleep(2000);
		ReleaseSemaphore(hObj, 1, &lCount);
		break;
	default:
		break;
	}
	myprintf("Thrd1 Exit!");
	return 0;
}

DWORD __stdcall ThrdFun2(LPVOID p)
{
	HANDLE hObj = OpenSyncObject((int)p);
	DWORD dwFlag;
	LONG lCount = 0;
	switch ((int)p)
	{
	case 1:
	    Sleep(2000);
		SetEvent(hObj);
		break;
	case 2:
		dwFlag = WaitForSingleObject(hObj, INFINITE);
		myprintf("Thrd2 Wait Success!");
		Sleep(2000);
		ReleaseMutex(hObj);
		break;
	case 3:
		//dwFlag = WaitForSingleObject(hObj, INFINITE);
		myprintf("Thrd2 Wait Success!");
		Sleep(2000);
		ReleaseSemaphore(hObj, 1, &lCount);
		break;
	default:
		break;
	}	
	myprintf("Thrd2 Exit!");
	return 0;
}

void CALLBACK TimeProc(LPVOID pArg, DWORD dwTimeLow, DWORD dwTimeHigh)
{
	printf("ThreadId: %d, Time: %d %d\n", GetCurrentThreadId(), dwTimeLow, dwTimeHigh);
}

int _tmain(int argc, _TCHAR* argv[])
{
	DWORD dwThrdId1, dwThrdId2;
	HANDLE h1, h2, hObj;
	for (int i = 1; i < 1; ++i)
	{
		hObj = CreateSyncObject(i);
		h1 = CreateThread(NULL, 0, ThrdFun1, (LPVOID)i, 0, &dwThrdId1);
		h2 = CreateThread(NULL, 0, ThrdFun2, (LPVOID)i, 0, &dwThrdId2);
		WaitForSingleObject(h1, INFINITE);
		WaitForSingleObject(h2, INFINITE);
		CloseHandle(hObj);
		CloseHandle(h1);
		CloseHandle(h2);
	}

	HANDLE hTime = CreateSyncObject(4);
	LARGE_INTEGER liTime;
	liTime.QuadPart = -10000000;
	DWORD dwTick;
	BOOL b = SetWaitableTimer(hTime, &liTime, 2000, TimeProc, NULL, FALSE);
	while (1)
	{
		dwTick = GetTickCount();
		SleepEx(INFINITE, TRUE);
		//WaitForSingleObject(hTime, INFINITE);
		dwTick = GetTickCount() - dwTick;
	}
	WaitForSingleObject(hTime, INFINITE);
	CloseHandle(hTime);
	printf("Time Wait Success!\n");
	system("@pause");
	return 0;
}


你可能感兴趣的:(Windows核心编程)