Im designing a program in c++ and im wondering the best way to implement its threading aspecs. Ive narrowed it down to three choices:
1. using pThread (this is a c library so not sure how that would work. Shouldnt be to difficult to do). It's also POSIX... ummm?
2. Boost libs. - not sure whether this will port. Also gotta think about distribution. But it does look 'nice'.
void hello_world() {
cout << "Hello world, I'm a thread!" << endl;
}
int main(int argc, char* argv[]) {
// start a new thread that calls the "hello_world" function
boost::thread my_thread(&hello_world);
// wait for the thread to finish
my_thread.join();
3. design my own thread class: This was taken from an old site. It's a bit basic but meh, opensource and that
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*){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
}
Basically i'd like to know your experiences of implementing threads in c++, what worked for you?
cheers