Creating threads

Started by
3 comments, last by Xeile 18 years, 6 months ago
Hello, I got some problem with the CreateThread(...) function. Oke, here is my situation:

// Header
class Dinges
{
public:
    void Start();
    DWORD WINAPI Run(LPVOID);
    void Stop();
private:
    HANDLE Thread;
    DWORD ThreadID;
}

// Code
void Dinges::Start()
{
    LPTHREAD_START_ROUTINE Temp;
    Temp = Run;   // <-- Error is refering to this line
    Thread = CreateThread(NULL, 0, Temp, NULL, 0, &dwThreadID);
}

void Dinges::Stop()
{
    TerminateThread(Thread, 0);
}

DWORD WINAPI DINGES::Run(LPVOID lpParam)
{
    // Do something
}


To give a bit more information about LPTHREAD_START_ROUTINE is defined:

// winbase.h
typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)(
    LPVOID lpThreadParameter
    );
typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;


I get the following error: error C2440: '=' : cannot convert from 'DWORD (__stdcall Input::* )(void *)' to 'LPTHREAD_START_ROUTINE' There is no context in which this conversion is possible Can anyone point where I'm doing wrong, please? Thank you, Xeile
Advertisement
You can't assign a pointer to member function to a pointer to a non-member function. Pointer to member functions usually aren't even the same size as pointers to regular functions. You need to create a normal or static member function in order to pass to the CreateThread() function.
Aha, so if I change it to...

// headerclass Dinges{public:    static DWORD WINAPI Run(LPVOID);}


...it should happely compile. When doing so, is it possible to access non-static class members?
Pass the object's address as the lpParameter argument to CreateThread, and then cast the argument to the Run() function back to your class type.
Oke, thank you!

This topic is closed to new replies.

Advertisement