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

📄 app2_4b.cpp

📁 利用platformbuild编译
💻 CPP
字号:
// App2_4b.cpp : Kernel Mutex demo
//

#include "stdafx.h"


// globals
DWORD  gData;
HANDLE hMutex;


DWORD Thread1Proc(LPVOID lpv)
{
	DWORD dwThreadData;

	while (TRUE)
	{
		WaitForSingleObject(hMutex, INFINITE);

		dwThreadData = *(DWORD *)lpv;
		dwThreadData += 1;
		Sleep(2000); // simulate work being done
		*(DWORD *)lpv = dwThreadData;

		ReleaseMutex(hMutex);

		RETAILMSG(1, (_T("1    %8X\r\n"), dwThreadData));
		Sleep(1000);
	}

	return 0;
}


DWORD Thread2Proc(LPVOID lpv)
{
	DWORD dwThreadData;

	while (TRUE)
	{
		WaitForSingleObject(hMutex, INFINITE);

		dwThreadData = *(DWORD *)lpv;
		dwThreadData += 1;
		Sleep(50); // simulate work being done
		*(DWORD *)lpv = dwThreadData;

		ReleaseMutex(hMutex);

		RETAILMSG(1, (_T(" 2     %8X\r\n"), dwThreadData));
		Sleep(250);
	}

	return 0;
}


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

	// create unnamed, cleared mutex
	hMutex = CreateMutex(NULL, FALSE, NULL);

	// create child threads
	hThread1 = CreateThread(
					NULL,
					0,
					(LPTHREAD_START_ROUTINE)Thread1Proc,
					(LPVOID*)&gData,
					0,
					&idThread1);
	hThread2 = CreateThread(
					NULL,
					0,
					(LPTHREAD_START_ROUTINE)Thread2Proc,
					(LPVOID*)&gData,
					0,
					&idThread2);

	// get thread priority
	hThread0 = GetCurrentThread();
	iPriority = CeGetThreadPriority(hThread0);
	RETAILMSG(1, (_T("Priority = %d\r\n"), iPriority));

	// set thread priority
	CeSetThreadPriority(hThread1, iPriority - 1);
	CeSetThreadPriority(hThread2, iPriority - 2);

	// infinite loop
	while (TRUE)
	{
		RETAILMSG(1, (_T("-- %8X\r\n"), gData));
		Sleep(10000);
	}

	return 0;
}




⌨️ 快捷键说明

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