Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
create a thread using a class method
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
r3pek
Retired Dev
Retired Dev


Joined: 17 Sep 2003
Posts: 568
Location: Lisbon - Portugal

PostPosted: Mon Jan 12, 2004 7:32 pm    Post subject: create a thread using a class method Reply with quote

hi... here's my problem:

i have a class named cChecker that have a method defined like this:
Code:
void *threadProcess(void *param);


on another method of the class, i make a call to pthread_create to execute that method:

Code:
void cChecker::testing() {
   pthread_t tId;
   pthread_attr_t attr;

   // get default attributes
   pthread_attr_init(&attr);
   // create the thread
   pthread_create(&tId, &attr, threadProcess, this);
};


the problem is that a have this error message when compiling:
argument of type `void*(cChecker::)(void*)' does not match `void*(*)(void*)

how can i solve this?!
Back to top
View user's profile Send private message
mahny
n00b
n00b


Joined: 27 Jul 2003
Posts: 15

PostPosted: Mon Jan 12, 2004 7:58 pm    Post subject: Re: create a thread using a class method Reply with quote

Ah the old object method as thread function problem. Your code won't work because you can't use the adress of object methods in that fashion, you have to use a static method as a thread function, send the object who's method you are going to call as data and then call your actual thread function from that object. If you don't the linker will complain.
Back to top
View user's profile Send private message
r3pek
Retired Dev
Retired Dev


Joined: 17 Sep 2003
Posts: 568
Location: Lisbon - Portugal

PostPosted: Mon Jan 12, 2004 8:07 pm    Post subject: Reply with quote

hummm.... could you give an example?
Back to top
View user's profile Send private message
far
Guru
Guru


Joined: 10 Mar 2003
Posts: 394
Location: Stockholm, Sweden

PostPosted: Mon Jan 12, 2004 8:37 pm    Post subject: Reply with quote

Perhaps you would be better off with a c++ thread lib, instead of using pthreads directly. There is for instance
Boost.Threads and the one included in gtkmm.
_________________
The Porthole Portage Frontend
Back to top
View user's profile Send private message
r3pek
Retired Dev
Retired Dev


Joined: 17 Sep 2003
Posts: 568
Location: Lisbon - Portugal

PostPosted: Mon Jan 12, 2004 8:48 pm    Post subject: Reply with quote

that ones work with class methods?
Back to top
View user's profile Send private message
HomerSimpson
l33t
l33t


Joined: 25 Jan 2003
Posts: 869
Location: Ohio, USA

PostPosted: Mon Jan 12, 2004 8:54 pm    Post subject: Reply with quote

There is also ACE and it is in portage @ dev-libs/ace
_________________
The strong must protect the Sweet.
Back to top
View user's profile Send private message
r3pek
Retired Dev
Retired Dev


Joined: 17 Sep 2003
Posts: 568
Location: Lisbon - Portugal

PostPosted: Mon Jan 12, 2004 9:44 pm    Post subject: Reply with quote

well... i think i did it.
the compiler/linker does not give errors anymore.

changes:
Code:
void cChecker::testing() {
   pthread_t tId;
   pthread_attr_t attr;

   // get default attributes
   pthread_attr_init(&attr);
   // create the thread
   pthread_create(&tId, &attr, cChecker::threadProcess, (void *)this);
};


Code:

void *cChecker::threadProcess(void *param) {
   cChecker *pObj = (cChecker *)param;

         ...
}


i think it's ok... or so a read.

can anyone give a opinion?[/code]
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum