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 ...
随机推荐
- 用于科创的git log美化输出
git log --reverse --pretty=format:'%cd %s' --date=short > a.txt 更好的: git log --reverse --pretty=f ...
- arcgis for android 无法加载本地jpg影像解决办法
因为jpg影像没有生成金子塔文件*.rrd 一个完整的JPG影像必须包括如下文件: K-50-96-(16).aux 辅助文件K-50-96-(16).jgw 坐标信息K-50-96-(16).j ...
- Apache加载PHP.ini顺序
网上找到一份关于Apache加载PHP.ini顺序的文档: (1) apache的httpd.conf中的PhpIniDir: (2) 注册表键值:HKEY_LOCAL_MACHINE->SOF ...
- FreeMarker笔记 第四章 其它
4.1 自定义指令 4.1.1 简介 自定义指令可以使用macro指令来定义.Java程序员若不想在模板中实现定义指令,而是在Java语言中实现指令的定义,这时可以使用freemarker.templ ...
- 【LeetCode】189 - Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
在一个Mysql表达式中使用嵌套查询,出现了这个错误.原因是内层select语句带有limit子句. 在网上查了下,有文章指出: 比如这样的语句是不能正确执行的. select * from ta ...
- 数据结构(12) -- 图的邻接矩阵的DFS和BFS
//////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...
- [WebService]之TCPMon的使用
TCPMon是apache下的一个项目,下载地址:http://ws.apache.org/commons/tcpmon/download.cgi (1)功能: TCPMon可以拦截客户与服务之间的H ...
- c++ List、Vector、Stack、Queue使用
一.List使用 引入头文件#include <list> List基本函数Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比 ...
- Numpy矩阵取列向量
>>> A=matrix("1 2;3 4") >>> A matrix([[1, 2], [3, 4]]) >>> A[:, ...