0

I have an array of objects and want each one to call a member function in a separate thread (so they run concurrently). I'm using _beginthreadex and can get it to work fine for a standard function but can't figure out the syntax to pass the member function to the _beginthreadex call. Here's an example of what I'm doing (the block of code after the second comment does not compile):

    #include <Windows.h>
    #include <process.h>
    #include <stdio.h>

    unsigned __stdcall mythread(void* data) { 
        printf("\nThread %d", GetCurrentThreadId()); 
        return 0; 
    }

    class myClass {
        public:
            unsigned __stdcall myClass::myThread(void* data);
    };

    unsigned __stdcall myClass::myThread(void* data) {
        printf("\nThread %d", GetCurrentThreadId()); 
        return(0);
    }

    int main(int argc, char* argv[]) { 

        int i, numThreads = 5;

        // this works
        HANDLE *myHandle = new HANDLE[numThreads];
        for(i=0;i<numThreads;i++) myHandle[i] = (HANDLE)_beginthreadex(0, 0, &mythread, 0, 0, 0);
        WaitForMultipleObjects(numThreads, myHandle, true, INFINITE); 
        for(i=0;i<numThreads;i++) CloseHandle(myHandle[i]); 
        getchar();
        delete myHandle;

        // this does not compile - not sure of syntax to call myObject[i].myThread in _beginthreadex
        HANDLE *myHandle2 = new HANDLE[numThreads];
        myClass *myObject = new myClass[numThreads];
        for(i=0;i<numThreads;i++) myHandle2[i] = (HANDLE)_beginthreadex(0, 0, &myObject[i].myThread, 0, 0, 0);
        WaitForMultipleObjects(numThreads, myHandle2, true, INFINITE); 
        for(i=0;i<numThreads;i++) CloseHandle(myHandle2[i]);  
        getchar();
        delete myObject;
        delete myHandle2;

        return 0; 
    }

Thanks in advance for any help!

rgames

Nikolay K
  • 3,770
  • 3
  • 25
  • 37
Richard Ames
  • 43
  • 1
  • 1
  • 4
  • The problem you are seeing is pretty much the same one as described here: http://stackoverflow.com/questions/1151582/pthread-function-from-a-class/ – Jeremy Friesner Apr 07 '15 at 03:58

1 Answers1

0
  • You cannot get address of member fuinction using

    &myObject[i].myThread
    

    correct way is like this

    &myClass::myThread
    
  • In C++ functions and member functions have different signature like

    /* pointer for this function would have type void(*)(int, float)  */
    void foo(int, float) { /* ... */ }
    
    class A {
    public:
        /* pointer for this function would have type void(A::*)(int, float)  */
        void foo(int, float) { /* ... */ }
    };
    

    they are have different type even they both return void and take int and float.

As you can see in msdn the _beginthread function takes function as

unsigned (__stdcall *)( void * )

but you are trying to pass

unsigned (__stdcall myClass::*)( void* )

So to correct your code you can do:

Method 1: you need to write another function, something like

unsigned __stdcall mymemberthread(void* data) {
    if (data != NULL) {
        myClass* m = (myClass*)data;
        m->myThread(m + 1);
    }
    return 0;
}

And create thread like

(HANDLE)_beginthreadex(0, 0, &mymemberthread, &myObject[i], 0, 0);

Method 2: make function myClass::myThread static

class myClass {
    public:
        static unsigned __stdcall myThread(void* data);
};
unsigned __stdcall myClass::myThread(void* data) {
    printf("\nThread %d", GetCurrentThreadId());
    if (data != NULL) {
        myClass* m = (myClass*)data;
        m->myThread(m + 1);
    }
    return(0);
}
...
(HANDLE)_beginthreadex(0, 0, &myClass::myThread, &myObject[i], 0, 0);

Method 3: if this is acceptabe use STL and std::thread, this will make you code more generic.

Nikolay K
  • 3,770
  • 3
  • 25
  • 37