473,498 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Posix Thread : C++ : poiinter to Member function

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
2 3581
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3004
by: stub | last post by:
Several objects of the same class, which contains a static member function, are created in several threads. If the said member function is called in the threads concurrently, it must be made to...
6
2630
by: Christian Buckl | last post by:
Hi, I try to implement my own thread class based on POSIX threads. I want my class to manage everything (creation of threads, exception handling...). This includes also some functions that need to...
4
2039
by: Clint Ruen | last post by:
Hello all, I have written out a data structure using the binary flag on an ofstream. The struct/class is something like this class SomeData { public: int data1;
6
2334
by: darklupine | last post by:
I'm working on a project for a class at school. In order to complete the project, I have to create a new thread, but I'm not overly certain on how to do so. I have it coded, but it still throws an...
5
1619
by: fniles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message...
23
5651
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
8
4384
by: JackC | last post by:
Hi, I am trying to get posix threads working from within an object, heres some code: int NConnection::TheadControl() { int thread_id; pthread_t new_connection;...
6
3860
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
1
1799
by: Ian Collins | last post by:
Chris M. Thomasson wrote: Why bother with all that nonsense when there is a standard solution? -- Ian Collins
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.