简介

上一篇文章 Windows Mobile使用.NET Compact Framework开发多线程程序 讲述了如何使用.NET Compact Framework进行多线程程序的开发,这篇讲述Native C++开发多线程程序的方法。

 

实现

环境

mutilthreading-native-cpp-1

mutilthreading-native-cpp-2

Environment: Visual Studio 2008 + Native C++ + WTL 8.1 + Windows Mobile 5.0 R2 professional (VS 2008 built-in) + ARMV4I

 

类定义 Class Definition

 

public:
void SendRequest();
void HandleRequest();

private:
HANDLE volatile mHandlerEvent;
HANDLE mHandlerThreadHnd;
DWORD mHandlerThreadId;

HANDLE mRequesterThreadHnd;
DWORD mRequesterThreadId;

MessageQueue mMessageQueue;
CRITICAL_SECTION mLook;

bool volatile mStarted;

private:
void UpdateMessageList(const CString& msg);
void StartThreading();
void StopThreading();

mHandlerEvent is the event to wake up handler thread.

mHandlerThreadHnd is the handler thread’s handle.

mHandlerThreadId is the handler thread’s Id

mMessageQueue is the shared resource. I use STL queue to encapsulate it.

mLook is a CRITICAL_SECTION object and is used to lock the shared resource.

mHandlerEvent  用于唤醒处理线程的事件。

mHandlerThreadHnd  线程句柄。

mHandlerThreadId 线程ID。

mMessageQueue STL的queue,共享资源,用于线程间分享数据。

mLook 是 CRITICAL_SECTION 对象,用于线程间的加锁。

 

Start two threads

//    Handler thread methods
DWORD WINAPI HanlderThreadProc(void *param)
{
if (param)
{
try
{
CThreadingDemoView* view = (CThreadingDemoView*)param;
view->HandleRequest();
}
catch(...)
{
}
}
return 0;
}

// Requester thread methods
DWORD WINAPI RequesterThreadProc(void *param)
{
if (param)
{
try
{
CThreadingDemoView* view = (CThreadingDemoView*)param;
view->SendRequest();
}
catch(...)
{
}
}
return 0;
}
void CThreadingDemoView::StartThreading()
{
if(!mStarted)
{
UpdateMessageList("Start threading...");
mStarted = true;

InitializeCriticalSection(&mLook);

mHandlerEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // manual reset, initial state is nonsignaled

mHandlerThreadHnd = CreateThread(NULL, 0, &HanlderThreadProc, this, CREATE_SUSPENDED, &mHandlerThreadId);
if (mHandlerThreadHnd)
{
SetThreadPriority(mHandlerThreadHnd, THREAD_PRIORITY_NORMAL);
ResumeThread(mHandlerThreadHnd);
}

mRequesterThreadHnd = CreateThread(NULL, 0, &RequesterThreadProc, this, CREATE_SUSPENDED, &mRequesterThreadId);
if (mRequesterThreadHnd)
{
SetThreadPriority(mRequesterThreadHnd, THREAD_PRIORITY_NORMAL);
ResumeThread(mRequesterThreadHnd);
}
}
}

Initialise some data members such mLook and mHandlerEvent. And start the requester thread and handler thread. I would like to pass through the class pointer to the global functions and use member function of the class to run the thread.

初始化各个变量。然后启动两个线程,启动线程的时候把本身对象指针传递到静态函数,这样处理的时候还是对象的方法来处理。 新线程会调用成员函数HandleRequest()和SendRequest()来运行,把所有的逻辑还是封装在同一个类里面。

 

Requester thread

void CThreadingDemoView::SendRequest()
{
int i = 0;
while(mStarted)
{
if(i > 100)
{
i = 0;
}

Message msg(i, "CPP");
EnterCriticalSection(&mLook);
mMessageQueue.push(msg);
LeaveCriticalSection(&mLook);

// Signal the event
SetEvent(mHandlerEvent);

CString s;
s.Format(_T("%8.8x - %d - %s"), ::GetCurrentThreadId(), msg.mID, msg.mMessageBody);
UpdateMessageList(s);

//sleep 500 milliseconds
Sleep(500);
++i;
}
//printf("Requester thread terminated.\n");
}

Create an object of Message and put it into the queue (mMessageQueue). Use EnterCriticalSection() and LeaveCriticalSection() to lock the shared resource and then wake the handler thread.

简单化处理,发送请求线程支持生成一个请求对象,然后加锁放到共享queue中,然后通过事件唤醒处理线程。

 

Handler thread

void CThreadingDemoView::HandleRequest()
{
while (mStarted)
{
// Wait for the incomming request
WaitForSingleObject(mHandlerEvent, INFINITE); //INFINITE
ResetEvent(mHandlerEvent);

//Use temp queue to decrease the lock duration.
MessageQueue tempMessageQueue;
EnterCriticalSection(&mLook);
while(!mMessageQueue.empty() && mStarted)
{
tempMessageQueue.push(mMessageQueue.front());
mMessageQueue.pop();
}
LeaveCriticalSection(&mLook);

//procoss the request
while(!tempMessageQueue.empty())
{
CString s;
s.Format(_T("%8.8x - %d - %s"), ::GetCurrentThreadId(), tempMessageQueue.front().mID, tempMessageQueue.front().mMessageBody);
UpdateMessageList(s);
tempMessageQueue.pop();
}

}
//printf("Handler thread terminated.\n");
}

The handler thread will sleep until get the event (mHandlerEvent). Put all the requests to temporary queue and release lock. Display the thread id and message information when process the request.

处理线程一直sleep直到给mHandlerEvent事件所唤醒,在现实处理中,处理对象可能时间比较长,所以为了减少锁的时间,把请求放到临 时queue中,然后再处理不用锁的临时queue。

 

Stop Threads

void CThreadingDemoView::StopThreading()
{
if(mStarted)
{
UpdateMessageList("Stop threading...");
mStarted = false;

// Signal the event
SetEvent(mHandlerEvent);

// Wait for the Thread to Die
WaitForSingleObject(mHandlerThreadHnd, INFINITE);
CloseHandle(mHandlerThreadHnd);

WaitForSingleObject(mRequesterThreadHnd, INFINITE);
CloseHandle(mRequesterThreadHnd);

DeleteCriticalSection(&mLook);
CloseHandle(mHandlerEvent);

mHandlerEvent = INVALID_HANDLE_VALUE;
mHandlerThreadHnd = NULL;
mHandlerThreadId = 0;
mRequesterThreadHnd = NULL;
mRequesterThreadId = 0;
}
}

发信号停止线程,清理各种对象。

 

一个Native C++版本的多线程同步以及通信程序完成了,可以对比 Windows Mobile使用.NET Compact Framework开发多线程程序 来看,非常欢迎拍板,请多拍拍,无论程序,英文描述等等,好让我不断改进,早日得到offer。

 

源代码 http://files.cnblogs.com/procoder/ThreadingDemo.zip

posts - 139, comments - 0, trackbacks - 0, articles - 0

Copyright © PHP博客