Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C++
Article

Member Threads

Rate me:
Please Sign up or sign in to vote.
4.75/5 (9 votes)
1 May 20022 min read 86K   998   43   14
A template function that makes creating threads that use class member functions easier.
*********** Member Threads *************

Introduction

It is often necessary, at least for the sake of clarity, to have a worker thread member of a class. Of course this is not possible, because member functions have a hidden parameter (the "this" pointer). A very easy workaround consists in declaring a static function that receives as a parameter the "this" pointer. After a casting, the proper member function can be called.

class CDemo
{
public:
    static DWORD WINAPI ThreadStub(LPVOID Parameter)
    {
        ((CDemo*)Parameter)->Thread();
    }
    void Thread()
    {
        for(int i=0;i<100;i++)
        {
            cout<<"Worker Thread: iteration n. "<<i<<endl;
        }
    }
};


int main(int argc, char* argv[])
{
    CDemo c;
    DWORD Dummy;
    HANDLE h=CreateThread(NULL,NULL,CDemo::ThreadStub,&c,NULL,&Dummy);
    WaitForSingleObject(h,INFINITE);
    return 0;
}
I find it, at the very least, a little awkward. Moreover, very often The first thing the Worker thread does is to fire an event. The father thread will be waiting for this event to make sure that the child has been actually born.

To avoid all this hassle, I created a very simple templated function, CreateMemberThread, that does all this work for us.

Following the same sample as above, we would replace the code in the

main() 
function like this:

#include "memberthreads.h"
class CDemo
{
//no static stub!
....
....
}
int main(int argc, char* argv[])
{
    CDemo c;
    HANDLE h=CreateMemberThread<CDemo>(&c,CDemo::Thread);
    WaitForSingleObject(h,INFINITE);
    return 0;
}
I find it nice that the CDemo class in the example must not be derived by another class, and that the syntax is very reminiscent of the original Windows API syntax. One other interesting feature, is that the name of the member function is completely arbitrary, and that more than one member thread function can be defined in a single class.

Let's go to the implementation:

The structure ThreadData<> holds a pointer to the instance of the class, a pointer to the member function, and a handle to an event to signal the start of the thread.

template <class T>
struct ThreadData
{
public:
    typedef void (T::*TFunc)();
    HANDLE hEvent;
    T* pThreadObject;
    TFunc pThreadFunc;
    static DWORD _ThreadFunc(ThreadData<T>* pThis)
    {
        //copying data, because after SetEvent is called,
        //the caller thread could restart and delete the
        //local data
        ThreadData<T> td=*pThis;
        SetEvent(td.hEvent);
        ((*(td.pThreadObject)).*(td.pThreadFunc))();
        return 0;
    }
};
The function CreateMemberThread<> allocates on the stack a ThreadData<> structure, fills it, create an event, spawns the thread and waits for the event to be fired. That's it.
template <class T>
HANDLE CreateMemberThread(T* p,void (T::*func)())
{
    ThreadData<T> td;
    td.pThreadObject=p;
    td.pThreadFunc=func;
    td.hEvent=CreateEvent(NULL,0,0,NULL);
    DWORD Dummy;   //To make win 9x Happy with the lpThreadId param
    HANDLE ThreadHandle=CreateThread(NULL,NULL,
                            (LPTHREAD_START_ROUTINE)ThreadData<T>::_ThreadFunc,
                            &td,NULL,&Dummy);
    WaitForSingleObject(td.hEvent,INFINITE);
    ::CloseHandle(td.hEvent);
    return ThreadHandle;
}

Of course, nothing new has been invented, but I think that this could be an elegant way of solving a trivial problem. No MFC is needed, no giant #includes (apart from a windows.h in your stdafx.h!) Enjoy it.

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


Written By
Web Developer
Italy Italy
Studied engineering in Milan, degreed
in 1998 or so, and since then he worked
in the Politecnico di Milano.
During the past three years he's been
hardly working mostly for TILAB doing
a sw Implementation of an MPEG4 Video
Codec kicking ass (IMHO) to DivX and
WMVideo.
Other interests are 3D analysis-synthesis,
VRML, and recently AAC Frown | :(

Comments and Discussions

 
GeneralQuetions about the Member Threads template Pin
-e26-Jun-06 4:06
-e26-Jun-06 4:06 
GeneralRe: Quetions about the Member Threads template Pin
Luca Piccarreta28-Jun-06 11:26
Luca Piccarreta28-Jun-06 11:26 
GeneralDebug build fine, Release build crashes Pin
Lee Hamel12-Apr-05 6:01
Lee Hamel12-Apr-05 6:01 
GeneralRe: Debug build fine, Release build crashes Pin
Lee Hamel13-Apr-05 12:01
Lee Hamel13-Apr-05 12:01 
Figured out the problem. It was a library of mine that I was linking to; it had a bug in it that caused the crash.
Generalgeneral static function template Pin
tuxyboy15-Nov-04 3:08
tuxyboy15-Nov-04 3:08 
GeneralRe: general static function template Pin
Luca Piccarreta16-Dec-04 23:31
Luca Piccarreta16-Dec-04 23:31 
GeneralRe: general static function template Pin
tuxyboy17-Dec-04 0:01
tuxyboy17-Dec-04 0:01 
GeneralNot XP after all Pin
Mike Osbahr6-Jun-02 8:26
Mike Osbahr6-Jun-02 8:26 
QuestionUsing this method with Windows XP? Pin
Mike Osbahr31-May-02 10:08
Mike Osbahr31-May-02 10:08 
AnswerRe: Using this method with Windows XP? Pin
Mike Osbahr4-Jun-02 11:05
Mike Osbahr4-Jun-02 11:05 
GeneralWell Done! Pin
14-May-02 0:22
suss14-May-02 0:22 
GeneralAnother similar class Pin
Anatoly Ivasyuk2-May-02 8:14
Anatoly Ivasyuk2-May-02 8:14 
GeneralNOT FREEWARE Pin
4-Jun-02 5:16
suss4-Jun-02 5:16 
GeneralSomething similar Pin
Todd Smith2-May-02 3:50
Todd Smith2-May-02 3:50 

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.