In article <1115961064.840078.59180@z14g2000cwz.googlegroups. com>,
raxitsheth@gmail.com wrote:
[color=blue]
> 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......
>[/color]
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