2010年3月6日 星期六

解決 _beginthread 無法用於 C++ member function 的問題

剛剛發現了別人寫的 code ,同樣是解決 _beginthread 不能直接進
入 C++ member function 的問題,不過它的作法真的比我漂亮許多。
##CONTINUE##

class ThreadX
{
public:

// In C++ you must employ a free (C) function or a static
// class member function as the thread entry-point-function.

static unsigned __stdcall ThreadStaticEntryPoint(void * pThis)
{
ThreadX * pthX = (ThreadX*)pThis; // the tricky cast
pthX->ThreadEntryPoint(); // now call the true entry-point-function

// A thread terminates automatically if it completes execution,
// or it can terminate itself with a call to _endthread().

return 1; // the thread exit code
}

void ThreadEntryPoint()
{
// This is the desired entry-point-function but to get
// here we have to use a 2 step procedure involving
// the ThreadStaticEntryPoint() function.
}
}

沒有留言: