I usually create an abstract class of sorts, and each thread that I create inherits from that class. Since a thread is implemented via a subclass, having to pass parameters directly to the thread function isn't necessary. Example:
Code:
// Obviously not complete, but this should demonstrate the basic idea....
class thread
{
public:
thread() : code_(0) { }
void start() { pthread_create(&pth_, NULL, thread::thread_router, reinterpret_cast<void*>(this)); }
void join() { pthread_join(&pth_, NULL); }
virtual int exec() = 0;
int exit_code() { return code_; }
private:
static void* thread_router(void* arg);
void exec_thread() { code_ = exec(); }
pthread_t pth_;
int code_;
};
void*
thread::thread_router(void* arg)
{
reinterpret_cast<thread*>(arg)->exec_thread();
return NULL;
}
class my_thread : public thread
{
public:
my_thread(int arg1, int arg2) : arg1_(arg1), arg2_(arg2) { }
virtual int exec();
private:
int arg1_, arg2_;
};
int
my_thread::exec()
{
// use arg1_, arg2_....
return 0;
}
my_thread thr(1, 2);
thr.start();
// Wait for it to complete...
thr.join();