Simple Worker Thread Class
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 PostThreadMessage, Join,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 (NotCreated, Created, Started, Restarted, Complete). 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的更多相关文章
- Worker Thread
http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...
- 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 ...
- 多线程 Worker Thread 模式
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
- 多线程系列之九:Worker Thread模式
一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...
- Exception thrown on Scheduler.Worker thread. Add `onError` handling
<html> <head></head> <body> java.lang.IllegalStateException: Exception throw ...
- Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server
MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...
- Mongodb之failed to create service entry worker thread
Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...
- Worker Thread模式
工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来 5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带 public class C ...
- Worker Thread等到工作来,来了就工作
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
随机推荐
- C#格式化小数位的方法
C#格式化字符串还是很方便的,之前一直没用过,也就没有缘分了解 Double dValue = 95.12345; int iValue = 10000; string strValue = &quo ...
- 实用js+css多级树形展开效果导航菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android定时三种方式
一.采用Handler与线程的sleep(long)方法二.采用Handler的postDelayed(Runnable, long)方法三.采用Handler与timer及TimerTask结合的方 ...
- MATLAB 通过二进制读写文件
这几天在做信息隐藏方面的应用,在读写文本文件时耗费许久,故特别的上网学习一二,这里给出一常用读写,其他的都类似. 很多时候,我们都要将一个.txt以二进制方式读出来,操作后在恢复成.txt文本. ma ...
- C++的类成员和类成员函数指针
类成员函数指针: 用于访问类成员函数,和一般函数指针有区别. 类成员函数处理的是类数据成员,声明类成员函数指针的同时,还要指出具体是哪个类的函数指针才可以.调用时也要通过对象调用. 而对于类的静态成员 ...
- 设计模式 适配器-Adapter
适配器模式:将一个类的接口,转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 直接上图.下面是对象适配器的类图.由于Java不支持多继承.所以这是Java的适配器实现方式. 结合H ...
- delphi 中怎么知道某一个月有多少天
if (month in (1,3,5,7,8,10,12)) return 31; else if (month in(4,6,9,11)) return 30; else if (year 是闰年 ...
- font-size的探究
整理网上的资料 font字体,用px,em,100%,rem?分什么情况考虑? 我们逐渐意识到,我们用px作为文字大小的单位,已经出现很多问题.最主要是体现在用户不能灵活的控制文字的大小. 对于大多数 ...
- phonegap 新窗口 WebView
自定义WebView窗口打开 import com.ap.work.QuickWeb public class QuickPlugin extends CordovaPlugin { /** * 新开 ...
- [原创]Devexpress XtraReports 系列 2 创建表格报表
昨天发表了Devexpress XtraReports系列开篇,今天我们继续. 今天的主题是创建表格报表. 首先我们来看看最后实现的效果.Demo最后附上. 接下来开始讲解如何一步一步做出这个报表: ...