简体   繁体   中英

Create new thread in class (windows)

I want to create a new thread in a class. The problem is when I need to pass a pointer to the function that will be used in the new thread. I am unable to pass a pointer to the function. A class function under the hood is basically this right?

void foo (this);

Then why does this code refuse to compile?

class TimeClass
{
private:
    DWORD   dwThreadId;
    HANDLE  hThread;

    LPTHREAD_START_ROUTINE Timer ();
public:
    TimeClass ();
};

TimeClass::TimeClass ()
{
    dwThreadId = CreateThread (NULL, 0, Timer, this, 0, &dwThreadId);
}

The signature of a thread function must be

DWORD WINAPI ThreadProc(LPVOID param);

An ordinary (ie nonstatic) C++ member function does not have the WINAPI calling convention so it cannot be used as a thread function. If you declare the member function as static then it can be used as a thread function:

static DWORD WINAPI ThreadProc(LPVOID param);

A class function under the hood is basically this right?

void foo (this);

Generally, no. It is what the compiler decides it to be, and there may be all kinds of 'non-virtual thunks', inlines, etc. The compiler is allowed to optimize the program in any way that doesn't change the program's behaviour, and such implementation details are not defined by the standard. That's why what you're trying to do is UB, and your best bet here (IMHO) would be something like:

extern "C" wrapper(void * p)
{
    static_cast<TimeClass*>(p)->whatever();
}

The ThreadProc() prototype is

DWORD WINAPI ThreadProc(
  _In_  LPVOID lpParameter
);

So you need to change the Timer() declaration like:

DWORD WINAPI Timer()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM