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

📄 thread.cpp

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 CPP
字号:
/*
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS HTTP Daemon
 * FILE:        thread.cpp
 * PURPOSE:     Generic thread class
 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
 * REVISIONS:
 *   CSH  01/09/2000 Created
 */
#include <debug.h>
#include <assert.h>
#include <windows.h>
#include <thread.h>

// This is the thread entry code
DWORD WINAPI ThreadEntry(LPVOID parameter)
{
	ThreadData *p = (ThreadData*) parameter;

    p->ClassPtr->Execute();

	SetEvent(p->hFinished);
	return 0;
}

// Default constructor
CThread::CThread()
{
	bTerminated = FALSE;
	// Points to the class that is executed within thread
	Data.ClassPtr = this;
	// Create synchronization event
	Data.hFinished = CreateEvent(NULL, TRUE, FALSE, NULL);

	// FIXME: Do some error handling
	assert(Data.hFinished != NULL);

	// Create thread
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadEntry, &Data, 0, &dwThreadId);

	// FIXME: Do some error handling
	assert(hThread != NULL);
}

// Default destructor
CThread::~CThread()
{
	if ((hThread != NULL) && (Data.hFinished != NULL)) {
		if (!bTerminated)
			Terminate();
		WaitForSingleObject(Data.hFinished, INFINITE);
		CloseHandle(Data.hFinished);
		CloseHandle(hThread);
		hThread = NULL;
	}
}

// Execute thread code
void CThread::Execute()
{
	while (!bTerminated) Sleep(0);
}

// Post a message to the thread's message queue
BOOL CThread::PostMessage(UINT Msg, WPARAM wParam, LPARAM lParam)
{
	return PostThreadMessage(dwThreadId, Msg, wParam, lParam);
}

// Gracefully terminate thread
void CThread::Terminate()
{
	bTerminated = TRUE;
}

// Returns TRUE if thread is terminated, FALSE if not
BOOL CThread::Terminated()
{
	return bTerminated;
}

⌨️ 快捷键说明

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