Click here to Skip to main content
11,804,919 members (68,352 online)
Click here to Skip to main content

Member Threads

, 1 May 2002 74.7K 881 42
Rate this:
Please Sign up or sign in to vote.
A template function that makes creating threads that use class member functions easier.
*********** Member Threads *************

<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--> <!-- Download Links -->

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


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
    static DWORD WINAPI ThreadStub(LPVOID Parameter)
    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);
    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);
    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
    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;
        return 0;
The function CreateMemberThread<> allocates on the stack aThreadData<> 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;
    DWORD Dummy;   //To make win 9x Happy with the lpThreadId param
    HANDLE ThreadHandle=CreateThread(NULL,NULL,
    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.


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

Luca Piccarreta
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
Other interests are 3D analysis-synthesis,
VRML, and recently AAC :(

You may also be interested in...

Comments and Discussions

GeneralQuetions about the Member Threads template Pin
-e26-Jun-06 4:06
member-e26-Jun-06 4:06 
GeneralRe: Quetions about the Member Threads template Pin
Luca Piccarreta28-Jun-06 11:26
memberLuca Piccarreta28-Jun-06 11:26 
GeneralDebug build fine, Release build crashes Pin
Lee Hamel12-Apr-05 6:01
memberLee Hamel12-Apr-05 6:01 
GeneralRe: Debug build fine, Release build crashes Pin
Lee Hamel13-Apr-05 12:01
memberLee Hamel13-Apr-05 12:01 
Generalgeneral static function template Pin
tuxyboy15-Nov-04 3:08
membertuxyboy15-Nov-04 3:08 
GeneralRe: general static function template Pin
Luca Piccarreta16-Dec-04 23:31
memberLuca Piccarreta16-Dec-04 23:31 
GeneralRe: general static function template Pin
tuxyboy17-Dec-04 0:01
membertuxyboy17-Dec-04 0:01 
GeneralNot XP after all Pin
Mike Osbahr6-Jun-02 8:26
memberMike Osbahr6-Jun-02 8:26 
QuestionUsing this method with Windows XP? Pin
Mike Osbahr31-May-02 10:08
memberMike Osbahr31-May-02 10:08 
AnswerRe: Using this method with Windows XP? Pin
Mike Osbahr4-Jun-02 11:05
memberMike Osbahr4-Jun-02 11:05 
Been working on this for a few days now. It appears that a try{} block inside one of the functions called by the thread is causing the error message. Reduced the called function to the following and still get:

'The application has requested to be terminated using an unusual method.'

void MyApp::fn(void)
ADO_RS *pAdoRs = new ADO_RS;
bool bTest = false;
if (bTest)
if (pAdoRs->IsOpen())
delete pAdoRs;

Added a try{} block to the console app in the demo code, but it does not produce the same error.

>>>-----> MikeO
GeneralWell Done! Pin
Anonymous14-May-02 0:22
memberAnonymous14-May-02 0:22 
GeneralAnother similar class Pin
Anatoly Ivasyuk2-May-02 8:14
memberAnatoly Ivasyuk2-May-02 8:14 
Mr Angry4-Jun-02 5:16
memberMr Angry4-Jun-02 5:16 
GeneralSomething similar Pin
Todd Smith2-May-02 3:50
memberTodd Smith2-May-02 3:50 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 2 May 2002
Article Copyright 2002 by Luca Piccarreta
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid