博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Mobile使用Native C++开发多线程程序
阅读量:5864 次
发布时间:2019-06-19

本文共 5202 字,大约阅读时间需要 17 分钟。

简介

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

 

实现

环境

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++版本的多线程同步以及通信程序完成了,可以对比  来看,非常欢迎拍板,请多拍拍,无论程序,英文描述等等,好让我不断改进,早日得到offer。

 

源代码 

 

可能是这个月最后一篇博文了,搬家没有宽带。

    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2010/03/11/Multithreading-native-cpp.html,如需转载请自行联系原作者

你可能感兴趣的文章
第二个shell(求和)
查看>>
打印后台程序服务没有运行,无法添加或使用打印机的处理方法
查看>>
按时间顺序显示指定目录的文件
查看>>
谈谈SQL Server高可用的常见问题
查看>>
WindowsPhone7真机部署和调试程序
查看>>
Linux出现Read-only file system
查看>>
sendmail源码包配置
查看>>
phpunit 单元测试案例--签到任务
查看>>
OGG logdump跳过某事物操作方法
查看>>
如何解决shell执行派生子shell问题
查看>>
VMware虚拟化--vsphere 5介绍
查看>>
SPL笔记之双向链表
查看>>
如何在macOS上为Python安装XGBoost
查看>>
MySQL中计算sql语句影响行数的函数
查看>>
Foundation-结构体
查看>>
Git:错误:error:src refspec master does not match any
查看>>
northropgrumman
查看>>
linux内核sysfs详解【转】
查看>>
关于链接文件的探讨
查看>>
android之LruCache源代码解析
查看>>