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 ...
随机推荐
- 树莓派 安装 OpenCV 使用CMake 编译工程 最新版2015
一.安装make,cmake sudo apt-get install make sudo apt-get install cmake 二.下载deb包 去这里下载libopencv_2.4.10.d ...
- bsp tree
http://www.cnblogs.com/dreams/archive/2007/03/25/687267.html http://blog.csdn.net/iduosi/article/det ...
- myeclipse10添加jQuery自动提示
首先先要在装上spket插件,这个网上有好多教程,我就不详细说了,主要说一下后面的设置,因为我发现我按照网上的装完也设置完没办法使用自动提示功能,以下是我根据前辈的经验然后自己摸索出来的: 选中所建的 ...
- XSS 前端防火墙(3):无懈可击的钩子
昨天尝试了一系列的可疑模块拦截试验,尽管最终的方案还存在着一些兼容性问题,但大体思路已经明确了: 静态模块:使用 MutationObserver 扫描. 动态模块:通过 API 钩子来拦截路径属性. ...
- 了解一下jsp
本着和大家共同学习jsp的原则,今天谈一谈jsp. 首先,JSP(全称JavaServer Pages)是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户 ...
- other 遮罩层
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 筛选DataTable数据的方法
对DataTable进行过滤筛选的一些方法Select,dataview 当你从数据库里取出一些数据,然后要对数据进行整合,你很容易就会想到: DataTable dt = new DataTable ...
- 前端复习-02-ajax原生以及jq和跨域方面的应用。
ajax这块我以前一直都是用现成的jq封装好的东西,而且并没有深入浅出的研究过,也没有使用过原生形式的实现.包括了解jsonp和跨域的相关概念但是依然没有实现过,其中有一个重要的原因我认为是我当时并不 ...
- CSS 居中大全
<center> text-align:center 在父容器里水平居中 inline 文字,或 inline 元素 vertical-align:middle 垂直居中 inline 文 ...
- SFTPTool 和 FTPTooL.java
两个工具类依赖的jar包: FTPTool.java public static void main(String[] args) throws Exception{ FTPTooL ftpTool ...