Click here to Skip to main content
Licence 
First Posted 30 Oct 2001
Views 77,566
Bookmarked 18 times

An easy to use worker thread

By | 30 Oct 2001 | Article
Use worker threads in your application without the complicated details

Sample Image - WorkerThread.gif

Introduction

I had the need for a worker thread which is easy to use and is independent of libraries like MFC. So I wrote a class called CWorkerThread which fits my needs. It provides functionality to execute an unlimited amount of functions in a single thread. The functions are processed syncronously in the order the events which are related to these functions are fired.

How it works

The CWorkerThread class has a method called AddEvent(HANDLE hEvent, EVENTPROC pProc, LPVOID pProcParam = NULL); This function takes a handle, a static or global function and an optional void pointer. The function pProc is executed when the event hEvent occurs. The pProcParam parameter is passed to that function. You can call AddEvent(...) for all events you want the thread to process. To start the thread call the objects Start(const int& nPriority = THREAD_PRIORITY_NORMAL); method. This will create the thread and initialize it's event queue. When you now set an event, the related function will be executed. To give you an impression of this event queue I pasted the code of the threads "main" thread procedure here:

UINT CWorkerThread::ThreadProc(LPVOID pProcParam)
{
    assert(pProcParam);

    CWorkerThread* pThread = reinterpret_cast<CWorkerThread*>(pProcParam);
    DWORD dwResult = 0;

    int nSize = pThread->m_arrEvents.size();
    HANDLE* pArrEvents = new HANDLE[nSize];
    for (int i = 0; i < nSize; i++)
        pArrEvents[i] = pThread->m_arrEvents[i].hEvent;

    // Install the event queue...
    while (true)
    {
        dwResult = ::WaitForMultipleObjects(nSize,
                                            pArrEvents,
                                            FALSE,
                                            INFINITE) - WAIT_OBJECT_0;

        if (dwResult == WORKERTHREADEVENT_KILL)
            break;
		
        // Execute appropriate function...
        (pThread->m_arrEvents[dwResult].pProc)(
             pThread->m_arrEvents[dwResult].pProcParam);
    }

    delete[] pArrEvents;
	
    ::SetEvent(pThread->m_hEventIsKilled);
    return 0;
}

Conclusion

The supplied sample application will show you how it works. If you need help with this or have suggestions on how to improve it or state bugs, feel free to drop me an email. Updated versions may be found at http://www.nitrobit.com/ or http://www.codecommunity.com/.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Frank Melber



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMaybe Demo....(not only one WorkerThread) Pinmemberplayer.2:49 20 Sep '06  
GeneralGreat work, got my 5 points! PinmemberLars [Large] Werner8:46 25 Aug '06  
GeneralNice Pinsussgem.tang16:11 26 Aug '05  
Generaladding and removing events PinmemberFatalError0x4c16:24 18 Feb '04  
GeneralRemoving events from queue PinmemberRicardoS0:29 11 Feb '02  
QuestionNot starting again ? PinmemberYves19:04 21 Nov '01  
Answerstarts perfectly... PinmemberPPM2:50 9 Feb '02  
GeneralRe: starts perfectly... PinmemberPathi Gorantla21:56 1 Apr '02  
GeneralRe: starts perfectly... PinmemberAnonymous6:15 15 Apr '02  
GeneralMinor correction Pinmembertorsten_hoff8:56 19 Nov '01  
GeneralQuestion PinmemberTodd Smith12:57 31 Oct '01  
GeneralRe: Question PinmemberFrank Melber0:25 1 Nov '01  
GeneralSome improvement ideas PinmemberKeith Bussell12:40 31 Oct '01  
GeneralRe: Some improvement ideas PinmemberTodd Smith13:34 31 Oct '01  
GeneralRe: Some improvement ideas PinmemberKeith Bussell6:43 1 Nov '01  
GeneralRe: Some improvement ideas PinmemberFrank Melber0:41 1 Nov '01  
GeneralRe: Some improvement ideas PinmemberKeith Bussell8:56 1 Nov '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 31 Oct 2001
Article Copyright 2001 by Frank Melber
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid