Re: how to make a class member function as the start function of
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Re: how to make a class member function as the start function of




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Re: how to make a class member function as the start function of  
David Logan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:08 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register