07-18-04 12:50 PM
googler wrote:
> how to make a class member function as the start function of a thread
>
> my code ::
>
> class thread
> {
> public:
> void * run( void *);
> void start()
> {
> pthreat_t tid;
> pthread_create( &tid, NULL, run, NULL);
> }
> };
>
> :::CC compiler , complains that it cannot perform a cast from void*(
> thread :: *) void(*) to void *(*)(void *)
>
> help me solve this
>
You can't do it that way. You need to have pthread_create start a
vanilla C function, with the address of the class instance as its
parameter. Then the vanilla C function calls the threads routine.
class thread {
void Start();
void *Run();
}
thread::Start() {
pthread_create(&tid,NULL,runthread,this);
}
void *runthread(void *args) {
thread *t = (thread *)args;
return t->Run();
}
Neither the syntax nor the args is necessarily 100%. I always have to
play around a little bit to get it just right, but this is the general idea.
David Logan
[ Post a follow-up to this message ]
|