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 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 0;
}

DWORD WINAPI OnComplete(void *param)
{
    int i = (int)param;

    printf("OnComplete data = %d\n", i);

    return 0;
}

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[2].RegisterOnCompleteRoutine(OnComplete, (void *)1234);

    // Start all of them
    for (int i = 0; i < MAXCOUNT; ++i) {
        if (workerThread[i].Start(ThreadProc, (void *)i)) {
            printf("Started %d\n", i);
        }
    }

    for (int i = 0; i < MAXCOUNT; ++i) {
        if (workerThread[i].ReExecute((void *)i)) {
            printf("Restarted %d\n", i);
        }

    }

    for (int i = 0; i < MAXCOUNT; ++i) {
        workerThread[i].End();
    }

    // main thread will wait here, till all others finish.
    for (int i = 0; i < MAXCOUNT; ++i) {
        workerThread[i].Join();
    }

    return 0;
}        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值