Posix Thread : C++ : poiinter to Member function

R

raxitsheth

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
 
J

Jack Klein

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.
 
H

Howard Hinnant

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top