Sign in to follow this  
Xeile

Creating threads

Recommended Posts

Xeile    256
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

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
Xeile    256
Aha, so if I change it to...


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



...it should happely compile. When doing so, is it possible to access non-static class members?

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this