By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

Posix Thread : C++ : poiinter to Member function

P: n/a
Hello All...

I am using Posix Thread.
class Parent
{
public: virtual void* func(void *)=0;
};

class Child : public
{
void *func(void *);
};
void* Child :: func(void *) { //pring Msg}

int GlobalFunc(Parent * p)
{
void* (Parent::*func)= p->func;//p May Point to Child Object....!!!


pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);// Here I want to Call
func of Any Derived Class of Parent
pthread_exit(0);
}

int main()
{

Child ch1;
GlobalFunc(&ch1);
return 1;
}


My Problem is at pthread_create......

in pthread_create i am Able to Call any Public function (Like C)
But I am not Able to compile the program when i am Calling any Function
of Any Class
using Function Pointer.
Error comes : cannot convert `void*(Parent ::*)(void*)' to
`void*(*)(void*)' for
argument `3' to `int pthread_create(pthread_t*, const
pthread_attr_t*,
void*(*)(void*), void*)'
i.e. pthread_create requires 3rd argument a Function pointer having Arg
and Ret type of void * , But I am Giving Function pointer which points
to Function of Parent type having Arg and Ret type void *


How to Solve the prob...
I think POSIX support only C,its not C++.......:(

Waiting for reply....
Thanks...

Regards
Raxit

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 12 May 2005 22:11:04 -0700, ra********@gmail.com wrote in
comp.lang.c++:
Hello All...

I am using Posix Thread.
POSIX and threads are off-topic here, they are not defined by the C++
language.

[snip]

My Problem is at pthread_create......

in pthread_create i am Able to Call any Public function (Like C)
But I am not Able to compile the program when i am Calling any Function
of Any Class
using Function Pointer.
That is correct. There is a difference between a free-standing
function and a non-static member function of a class. A pointer to
one is not the same thing as a pointer to the other, and there is no
conversion between the two.
Error comes : cannot convert `void*(Parent ::*)(void*)' to
`void*(*)(void*)' for
argument `3' to `int pthread_create(pthread_t*, const
pthread_attr_t*,
void*(*)(void*), void*)'
i.e. pthread_create requires 3rd argument a Function pointer having Arg
and Ret type of void * , But I am Giving Function pointer which points
to Function of Parent type having Arg and Ret type void *
That's because a pointer to a C++ member function requires more than
just the address of the function to call it. There must also be an
object of the class to invoke it on.
How to Solve the prob...
Read the FAQ for this group, link in my signature. In general there
is no way to make a callback function that deals with generic function
pointers work with a C++ member function. But there are work arounds,
and the FAQ suggests several of them.
I think POSIX support only C,its not C++.......:(
POSIX has nothing to do with language, it supports FORTRAN, COBOL,
Pascal, and any language at all that provides a binding to it. But
POSIX and pthreads are off-topic here.
Waiting for reply....


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2

P: n/a
In article <11*********************@z14g2000cwz.googlegroups. com>,
ra********@gmail.com wrote:
I am using Posix Thread.

class Parent
{
public: virtual void* func(void *)=0;
};

class Child : public
{
void *func(void *);
};
void* Child :: func(void *) { //pring Msg}

int GlobalFunc(Parent * p)
{
void* (Parent::*func)= p->func;//p May Point to Child Object....!!!

pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);// Here I want to Call
func of Any Derived Class of Parent
pthread_exit(0);

}

int main()
{

Child ch1;
GlobalFunc(&ch1);
return 1;
}

My Problem is at pthread_create......


You might want to investigate boost::threads and boost::bind
(www.boost.org). The thread package is a fairly lightweight wrapper
around pthreads (on a pthreads platform), and boost::bind is a function
adaptor which makes using pointer to member functions far less painful.

boost::bind has been included in the first C++ library technical report
(TR1), and you may soon start seeing it show up with your compiler under
namespace std::tr1.

The C++ committee is also very interested in threading support for
C++0X. Boost threads is one of the libraries that are being closely
looked at. Dinkumware and Metrowerks already ship libraries very
similar to the boost::threads package.

Here is your code recast to use these facilities with the Metrowerks
compiler(*):

#include <msl_thread>
#include <bind>

class Parent
{
public: virtual void* func(void *)=0;
};

class Child : public Parent
{
void *func(void *);
};
void* Child :: func(void *) {/*pring Msg*/ return 0;}

int GlobalFunc(Parent * p)
{
Metrowerks::thread t(std::tr1::bind(&Parent::func, p, (void*)0));
//...
t.join();
return 0;
}

int main()
{
Child ch1;
GlobalFunc(&ch1);
return 1;
}

*Disclaimer (std::tr1::bind not quite yet released, use boost::bind as a
workaround in current releases).

Unfortunately the return value from Child will be ignored. That problem
is currently under discussion and may be addressed in a standardized
threading package. In the meantime, if the return value is important to
your program, you will have to communicate it through other means (such
as storing the value within Parent/Child).

-Howard
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.