Click here to Skip to main content
13,629,058 members
Click here to Skip to main content
Add your own
alternative version

Stats

89.9K views
1K downloads
20 bookmarked
Posted 30 Oct 2001
Licenced

An easy to use worker thread

, 30 Oct 2001
Rate this:
Please Sign up or sign in to vote.
Use worker threads in your application without the complicated details
<!-- Download Links --> <!-- Article image -->

Sample Image - WorkerThread.gif

<!-- Add the rest of your HTML here -->

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

Share

About the Author

Frank Melber
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMaybe Demo....(not only one WorkerThread) Pin
player.20-Sep-06 2:49
memberplayer.20-Sep-06 2:49 
GeneralGreat work, got my 5 points! Pin
Lars [Large] Werner25-Aug-06 8:46
memberLars [Large] Werner25-Aug-06 8:46 
GeneralNice Pin
gem.tang26-Aug-05 16:11
sussgem.tang26-Aug-05 16:11 
Generaladding and removing events Pin
FatalError0x4c18-Feb-04 16:24
memberFatalError0x4c18-Feb-04 16:24 
GeneralRemoving events from queue Pin
RicardoS11-Feb-02 0:29
memberRicardoS11-Feb-02 0:29 
QuestionNot starting again ? Pin
Yves21-Nov-01 19:04
memberYves21-Nov-01 19:04 
Answerstarts perfectly... Pin
PPM9-Feb-02 2:50
memberPPM9-Feb-02 2:50 
GeneralRe: starts perfectly... Pin
Pathi Gorantla1-Apr-02 21:56
memberPathi Gorantla1-Apr-02 21:56 
GeneralRe: starts perfectly... Pin
Anonymous15-Apr-02 6:15
memberAnonymous15-Apr-02 6:15 
GeneralMinor correction Pin
torsten_hoff19-Nov-01 8:56
membertorsten_hoff19-Nov-01 8:56 
GeneralQuestion Pin
Todd Smith31-Oct-01 12:57
memberTodd Smith31-Oct-01 12:57 
GeneralRe: Question Pin
Frank Melber1-Nov-01 0:25
memberFrank Melber1-Nov-01 0:25 
GeneralSome improvement ideas Pin
Keith Bussell31-Oct-01 12:40
memberKeith Bussell31-Oct-01 12:40 
GeneralRe: Some improvement ideas Pin
Todd Smith31-Oct-01 13:34
memberTodd Smith31-Oct-01 13:34 
GeneralRe: Some improvement ideas Pin
Keith Bussell1-Nov-01 6:43
memberKeith Bussell1-Nov-01 6:43 
GeneralRe: Some improvement ideas Pin
Frank Melber1-Nov-01 0:41
memberFrank Melber1-Nov-01 0:41 
GeneralRe: Some improvement ideas Pin
Keith Bussell1-Nov-01 8:56
memberKeith Bussell1-Nov-01 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Cookies | Terms of Use | Mobile
Web04 | 2.8.180712.1 | Last Updated 31 Oct 2001
Article Copyright 2001 by Frank Melber
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid