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

📄 kevlab.cpp

📁 微软Windows CE开发实例程序!Embedded Visual C++ 4.0!
💻 CPP
字号:
// KEVLab.cpp : Defines the entry point for the application.
//

#include "stdafx.h"


DWORD WINAPI Thread_One(LPVOID lpone);
DWORD WINAPI Thread_Two(LPVOID lptwp);
DWORD WINAPI Thread_Three(LPVOID lpthree);


int u=1;  // control variable
HANDLE hMutex;
DWORD dwDuration = 0;


int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{

	int abc;
	DWORD tid1, tid2, tid3;
	HANDLE ct, t1, t2, t3;



// Get the thread work-duration from the registry

HKEY regkey = NULL;
LPDWORD regtype =0;
DWORD dwSize = 4;

// Open the Reg Key
 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
				  _T("Software\\Microsoft\\MSDNTrain\\KevLab"), 
				  0, 0, &regkey) == ERROR_SUCCESS)
 {
// Get the Value
	RegQueryValueEx(regkey, _T("Duration"), 
					NULL, regtype, (LPBYTE)&dwDuration , &dwSize);

 }
 else {
	 // Problem reading the Key Use Default Value
	dwDuration = 10000;
 }
// Close the Reg Key
RegCloseKey(regkey);

if ( (dwDuration <=0))
{
dwDuration = 10000;
}

	
	// Parse the command line

	LPTSTR szTP1, szTP2, szTP3;  //Strings for the thread priorities
	int tp1, tp2, tp3, maintp;

	szTP1 = _tcstok(lpCmdLine, _T(" "));
	if (szTP1) 
	tp1 = _ttoi(szTP1);
	else tp1 = 5;

	szTP2 = _tcstok(NULL, _T(" "));
	if (szTP2) 
	tp2 = _ttoi(szTP2);
	else tp2 = 5;

	szTP3 = _tcstok(NULL, _T(" "));
	if (szTP3) 
	tp3 = _ttoi(szTP3);
	else tp3 = 5;
	

	// Default priority back to 5, if given priority falls out of range.
	if (!((tp1<=255) && (tp1 >=0))) tp1 = 5;   
	if (!((tp2<=255) && (tp2 >=0))) tp2 = 5;   
	if (!((tp3<=255) && (tp3 >=0))) tp3 = 5;   

	// set the main thread prioritiy to the highest priority thread 
	maintp = tp1;
	if (tp2 < maintp) maintp = tp2;
	if (tp3 < maintp) maintp = tp3;

	if (maintp != 0) maintp--;  //up the maintp by one. 

	
	//Create an unnamed, cleared Mutex
	hMutex = CreateMutex(NULL, FALSE, NULL); 

	// Create three child threads
	t1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread_One, (LPVOID*)abc ,0, &tid1);
	t2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread_Two, (LPVOID*)abc ,0, &tid2);
	t3 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread_Three, (LPVOID*)abc ,0, &tid3);

	ct = GetCurrentThread();

	// Set the thread priorities to make it easier to watch the threads in Kernel Tracker.  
	// We set the Priority to the Main thread first so the child threads don't take over.
	if (!CeSetThreadPriority(ct, maintp)) OutputDebugString(_T("Could not Set Main Thread Priority"));
	if (!CeSetThreadPriority(t1, tp1)) OutputDebugString(_T("Could not Set Thread 1 Priority"));
	if (!CeSetThreadPriority(t2, tp2)) OutputDebugString(_T("Could not Set Thread 2 Priority"));
	if (!CeSetThreadPriority(t3, tp3)) OutputDebugString(_T("Could not Set Thread 3 Priority"));
	
	while(u<40)
	{
	if (u>=30) u++; 
	Sleep(100);
	}

	OutputDebugString(_T("Kevlab complete."));
	return 0;
}

// These are the ThreadProcs, each of the threads have a distinct lifetime 
// and treminate at various times (as per the control variable u)
// You can View this app with Kernel Tracker to get a good visual of the 
// thread interaction.


DWORD WINAPI Thread_One(LPVOID lpone)
{
while(u<10)
{
	WaitForSingleObject(hMutex, INFINITE);  // Wait until we have our Mutex
	DWORD x=0;

	while(x < dwDuration)
	{
		x++;   // do some work...
	}
	u++;
	ReleaseMutex(hMutex);   // Let someone else run.
}
	return 0;

}

DWORD WINAPI Thread_Two(LPVOID lptwo)
{
while(u<20)
{

	WaitForSingleObject(hMutex, INFINITE);  // Wait until we have our Mutex

	DWORD x=0;

	while(x < dwDuration)
	{
		x++;   // do some work...
	}
	u++;
	ReleaseMutex(hMutex);   // Let someone else run.

}	
return 0;
}

DWORD WINAPI Thread_Three(LPVOID lpthree)
{
while(u<30)
{

	WaitForSingleObject(hMutex, INFINITE);  // Wait until we have our Mutex
	DWORD x=0;

	while(x < dwDuration)
	{
		x++;  // do some work...
	}
	u++;
	ReleaseMutex(hMutex);   // Let someone else run.


}
return 0;
}

⌨️ 快捷键说明

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