⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app2_4c.cpp

📁 利用platformbuild编译调试
💻 CPP
字号:
// App2_4c.cpp : Kernel Unnamed Event demo
//

#include "stdafx.h"


// globals
int gTick;
HANDLE hEvent1;
HANDLE hEvent2;
BOOLEAN fRunning;


DWORD Thread1Proc(LPVOID lpv)
{
	static int iCount = 0;
	while (fRunning)
	{
		iCount++;
		WaitForSingleObject(hEvent1, INFINITE); // auto reset
		RETAILMSG(1, (_T("T1   %d\r\n"), iCount));
	}
	return 0;
}


DWORD Thread2Proc(LPVOID lpv)
{
	static int iCount = 0;
	HANDLE hWaitList[2] = {hEvent1, hEvent2};
	while (fRunning)
	{
		iCount++;
		WaitForMultipleObjects(sizeof(hWaitList)/sizeof(*hWaitList),
			hWaitList, FALSE, INFINITE);
		RETAILMSG(1, (_T("T2     %d\r\n"), iCount));

		Sleep(2000); // two seconds delay
	}
	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
	int iPriority;
	HANDLE hThread0;
	HANDLE hThread1;
	HANDLE hThread2;

	// create event objects
	hEvent1 = CreateEvent(NULL, FALSE, FALSE, NULL); // auto reset
	hEvent2 = CreateEvent(NULL, TRUE, FALSE, NULL); // manual reset
	fRunning = TRUE;

	// create child threads
	hThread1 = CreateThread(
					NULL,
					0,
					(LPTHREAD_START_ROUTINE)Thread1Proc,
					NULL,
					0,
					NULL);
	hThread2 = CreateThread(
					NULL,
					0,
					(LPTHREAD_START_ROUTINE)Thread2Proc,
					NULL,
					0,
					NULL);

	// thread priority
	hThread0 = GetCurrentThread();
	iPriority = CeGetThreadPriority(hThread0);
	RETAILMSG(1, (_T("Primary = %d\r\n"), iPriority));
	iPriority -= 1; // boost priority
	CeSetThreadPriority(hThread1, iPriority);
	RETAILMSG(1, (_T("Thread1 = %d\r\n"), iPriority));
	iPriority -= 1; // boost priority
	CeSetThreadPriority(hThread2, iPriority);
	RETAILMSG(1, (_T("Thread2 = %d\r\n"), iPriority));

	// main loop
	gTick = 0;
	while (gTick < 50) // limit runtime
	{
		gTick++;
		RETAILMSG(1, (_T("-- %d\r\n"), gTick));
		if ((gTick % 3) == 0) // every third
		{
			RETAILMSG(1, (_T("E1 auto\r\n")));
			PulseEvent(hEvent1);
		}
		if ((gTick % 5) == 0) // every tenth
		{
			RETAILMSG(1, (_T("E2 manual\r\n")));
			PulseEvent(hEvent2);
		}
		Sleep(1000); // one second
	}

	// shutdown threads
	RETAILMSG(1, (_T("-- %d shutdown\r\n"), gTick));
	fRunning = FALSE;
	Sleep(10000); // ten seconds

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -