Original Post
Hi in my mission to understand graphics and game programming in C++ I am getting to the point where I am going to have to learn all about threads, so because of this I have been reading a good article at http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-18.html which is explaining the basics ......It mentions that: I need to specify a entry point function for the thread We have to do a few tricks to wrap a C++ class around it because the entry function cannot be a normal member function of a class. it can be a static member function of a class "Static member functions do not have access to the this pointer of a C++ object. They can only access static data. " when the article surprises me is when it says Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. This has me scratching my head a bit as it sounds like its passing data into a void function? I submit the code below but do not unbderstand the reasoning of how and why this would work? could someone explain what is happening in the below code and how c++ allows arguments to void functions ......Im trying to look closely at the way the argument was passed and figure out the logic behind it. Thanks :-)
class Thread
{
public:
Thread();
int Start(void * arg);
protected:
int Run(void * arg);
static void * EntryPoint(void*);
virtual void Setup();
virtual void Execute(void*);
void * Arg() const {return Arg_;}
void Arg(void* a){Arg_ = a;}
private:
THREADID ThreadId_;
void * Arg_;
};
Thread::Thread() {}
int Thread::Start(void * arg)
{
Arg(arg); // store user data
int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
return code;
}
int Thread::Run(void * arg)
{
Setup();
Execute( arg );
}
/*static */
void * Thread::EntryPoint(void * pthis)
{
Thread * pt = (Thread*)pthis;
pthis->Run( Arg() );
}
virtual void Thread::Setup()
{
// Do any setup here
}
virtual void Thread::Execute(void* arg)
{
// Your code goes here
}