View previous topic :: View next topic |
Author |
Message |
r3pek Retired Dev
Joined: 17 Sep 2003 Posts: 568 Location: Lisbon - Portugal
|
Posted: Mon Jan 12, 2004 7:32 pm Post subject: create a thread using a class method |
|
|
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 |
|
|
mahny n00b
Joined: 27 Jul 2003 Posts: 15
|
Posted: Mon Jan 12, 2004 7:58 pm Post subject: Re: create a thread using a class method |
|
|
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 |
|
|
r3pek Retired Dev
Joined: 17 Sep 2003 Posts: 568 Location: Lisbon - Portugal
|
Posted: Mon Jan 12, 2004 8:07 pm Post subject: |
|
|
hummm.... could you give an example? |
|
Back to top |
|
|
far Guru
Joined: 10 Mar 2003 Posts: 394 Location: Stockholm, Sweden
|
Posted: Mon Jan 12, 2004 8:37 pm Post subject: |
|
|
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 |
|
|
r3pek Retired Dev
Joined: 17 Sep 2003 Posts: 568 Location: Lisbon - Portugal
|
Posted: Mon Jan 12, 2004 8:48 pm Post subject: |
|
|
that ones work with class methods? |
|
Back to top |
|
|
HomerSimpson l33t
Joined: 25 Jan 2003 Posts: 869 Location: Ohio, USA
|
Posted: Mon Jan 12, 2004 8:54 pm Post subject: |
|
|
There is also ACE and it is in portage @ dev-libs/ace _________________ The strong must protect the Sweet. |
|
Back to top |
|
|
r3pek Retired Dev
Joined: 17 Sep 2003 Posts: 568 Location: Lisbon - Portugal
|
Posted: Mon Jan 12, 2004 9:44 pm Post subject: |
|
|
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 |
|
|
|