Creating Thread Within Object?

Having some trouble creating a thread within an object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

NetworkManager::NetworkManager(unsigned short serverPort)
{

    run = 1;

    run = createSocket();

    sockaddr_in sockAddr;

    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = htons(serverPort);
    sockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

    run = connectToServer(sockAddr);

    //Start Thread

    unsigned int* threadID;

    _beginthreadex(NULL, 0, threadfunc, 0, 0, threadID);

}

....

unsigned __stdcall NetworkManager::threadfunc(void * param)
{


    //Gotta Put Code Here

    cleanUp();

    return 0;


}


error: argument of type `unsigned int (NetworkManager::)(void*)' does not match `unsigned int (*)(void*)'|

Any help would be greatly appreciated,

Nick :)
_bugintheadex must be taking a pointer to normal function. Try using functor.
take the threadfunc out of the NetworkManager class and leave it as a global function.

You can add threadfunc as a friend of the NetworkManager class and pass a pointer to it in the _beginthreadex function.

...
_beginthreadex(NULL, 0, threadfunc, 0, 0, (void*)this);
...

unsigned __stdcall threadfunc(void * param)
{
NetworkManager *pNM = (NetworkManager*)param;
}
Good ans ireshagun, just wondering if there is another way to do it? I thought the NetworkManager:: was just specifying that this method belongs to the NetworkManager class, I guess I don't understand why the types don't match.

Any help is awesome,

Thanks again, Nick :)
Topic archived. No new replies allowed.