http://www.codeproject.com/Articles/36184/Simple-Worker-Thread-Class

Introduction

Many times we need to create worker threads and generally we want to wait in the main thread till all worker threads finish their execution, something similar to pthread’s pthread_join call. We want some notification on the completion of worker thread; check the status of the worker thread if it is still executing in thread function or finished, etc. Also sometimes we want to keep the worker thread alive even on completion of thread function so that we can resubmit a different job (typically in a thread pool). All these things can be done with event objects by managing their state to signalled non signaled, etc. It is difficult to manage such code using different event objects. I consolidated all this commonly used worker thread functionality in a basic implementation of WorkerThread class.

WorkerThread Class

WorkerThread is a basic implementation for worker thread with PostThreadMessageJoin,RegisterOnCompleteRoutine and thread execution status. It can be enhanced further for many other features but I want to keep the idea simple. Here my main focus is just to show how to use a PostThreadMessage for worker threads.

Using the Code

Using WorkerThread class in your existing application is very simple. You just need to add WorkerThread.cpp in your project and include WorkerThread.h where you want to use this class.

Add variables of WorkerThread class wherever you want to create a worker thread. Create a worker thread usingStart() with an optional auto quit parameter (default is true) and to end the thread, use End() method. If auto quit parameter is true, there is no need to call End() method of WorkerThread class. Join() method will simply cause calling thread to wait till work thread finishes its execution. GetStatus() will let you know the current thread status (NotCreatedCreatedStartedRestartedComplete). ReExecute() method can be used only for non auto quit threads (created with false parameter in constructor) with different or same data (this can be further enhanced to avoid overwriting the data). RegisterOnCompleteRoutine() method can be used to register an optional routine that will be called on completion of thread function.

Below is the sample code to show the usage of the WorkerThread class:

#include "stdafx.h"
#include "WorkerThread.h" #define MAXCOUNT 5 DWORD WINAPI ThreadProc(void *param)
{
int i = (int)param;
//
// your code
// return ;
} DWORD WINAPI OnComplete(void *param)
{
int i = (int)param; printf("OnComplete data = %d\n", i); return ;
} int _tmain(int argc, _TCHAR* argv[])
{
// Create few worker threads with autoQuit false
WorkerThread workerThread[MAXCOUNT] = {false, false, false, false, false}; // Register an optional completion routine
workerThread[].RegisterOnCompleteRoutine(OnComplete, (void *)); // Start all of them
for (int i = ; i < MAXCOUNT; ++i) {
if (workerThread[i].Start(ThreadProc, (void *)i)) {
printf("Started %d\n", i);
}
} for (int i = ; i < MAXCOUNT; ++i) {
if (workerThread[i].ReExecute((void *)i)) {
printf("Restarted %d\n", i);
} } for (int i = ; i < MAXCOUNT; ++i) {
workerThread[i].End();
} // main thread will wait here, till all others finish.
for (int i = ; i < MAXCOUNT; ++i) {
workerThread[i].Join();
} return ;
}

Simple Worker Thread Class的更多相关文章

  1. Worker Thread

    http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...

  2. Do waiting or suspended tasks tie up a worker thread?

      https://blogs.msdn.microsoft.com/askjay/2012/07/29/do-waiting-or-suspended-tasks-tie-up-a-worker-t ...

  3. 多线程 Worker Thread 模式

    Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...

  4. 多线程系列之九:Worker Thread模式

    一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...

  5. Exception thrown on Scheduler.Worker thread. Add `onError` handling

    <html> <head></head> <body> java.lang.IllegalStateException: Exception throw ...

  6. Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server

    MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...

  7. Mongodb之failed to create service entry worker thread

    Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...

  8. Worker Thread模式

    工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来 5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带 public class C ...

  9. Worker Thread等到工作来,来了就工作

    Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...

随机推荐

  1. 【转】IOS --- OC与Swift混编

    群里大神发的网址,感觉有用就先收录了,暂时没时间看SWIFT,感觉代码简洁,但是可阅读性不是太高,有些代码让系统去判断类型,同样的,我们看代码的时候也得自己去判断类型,或许看多就习惯了,有时间再说吧, ...

  2. Amarino例程无法使用的问题

    Serial.begin(9600); 而不是用它的57600

  3. web医疗影像浏览demo及地址

    各种web影像浏览demo及地址1.WPACS http://demo.dayisheng.com 帐号密码 cc dd(http://demo.dayisheng.com/wpacs33.aspx? ...

  4. Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  5. android启动页优化实践

    最近观看友盟统计发现App启动页跳出率很高,观看启动页的平均启动时间为2.8s,如下图: 所以一个很现实的问题就出来了,优化启动页面... (一)做了什么? 我们的启动页面主要用于展示启动页面,加载网 ...

  6. 【Linux】Mutex互斥量线程同步的例子

    0.互斥量  Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...

  7. grails下的httpclient

    httpclient已经发布到4.3版本了,其中的API发生了巨大的变化,尤其是对于关闭连接显得更加容易理解,比如引入了CloseableHttpResponse. 然而grails2.3.2中自带了 ...

  8. SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...

  9. Tcl之group arguments

    1 doubel quotes This allows substitutions to occur within the quotations - or "interpolation&qu ...

  10. iOS 后台退出app时不执行applicationWillTerminate的临时解决方法

    - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release s ...