Re: Basic Question on POSIX Threads
Laurent D.A.M. MENTEN wrote:
Here is a skeleton of how I handle pthreads (the same pattern works with
win32 threads) in C++; Ther is of course a lot more to write for the
thread management itself but the importants things are there. I use it
by writting a derived class that overload the run() method.
Hope it helps.
class Thread
{
static void* glue( void* );
private:
pthread_t tid;
pthread_attr_t attr;
public:
Thread();
virtual void* run() = 0;
};
void* Thread::glue( void* t )
{
return ((Thread*)t)->run();
}
Thread::Thread()
{
// ...
pthread_create( &this->tid, &this->attr, Thread::glue, (void*)this );
// ...
}
This is wrong, Thread::glue is not an extern "C" function, so it
shouldn't be passed to pthread_create. You might get away with it (I'd
expect at least a complier warning), but it isn't portable.
I don't see why people are so hung up about passing a free function to
pthread_create in C++.
--
Ian Collins.