473,394 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 3577
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.