473,387 Members | 1,798 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,387 software developers and data experts.

Function pointer to member functions

Hi,

I was writing a simple multi-threaded program (code below). My intent is to
invoke a member function of a class that creates two threads - each thread
executes another member function of the same class. The pthread_create
function used to create a thread requires a pointer to the function that
will be executed. I tried all the possible syntax ideas that came to mind,
but I am not able to pass it a pointer to the member function of the class.
Can someone please point out my error. Thanks.

class test
{
public:
void* funcA(void* dud)
{
int i=0;
char c,*d;
d=(char*)dud;
c=*d;
for(i=0;i<100;i++)
printf("%c",c);
}

int funcB()
{
pthread_t threadId1,threadId2;
char c,d;
c='a';
pthread_create(&threadId1,NULL,this->funcA,&c); //I keep getting
compilation errors for this line. I tried a::funcA, &a::funcA, funcA,
&funcA. None of them work.
d='b';
pthread_create(&threadId2,NULL,this->funcA,&d); //I keep getting
compilation errors for this line
pthread_join(threadId2,NULL);
pthread_join(threadId1,NULL);
perror("say what");
}
};

main()
{
test obj;
obj.funcB();
}
Jul 22 '05 #1
3 2079
Rookie wrote:
Hi,

I was writing a simple multi-threaded program (code below). My intent is to
invoke a member function of a class that creates two threads - each thread
executes another member function of the same class. The pthread_create
function used to create a thread requires a pointer to the function that
will be executed. I tried all the possible syntax ideas that came to mind,
but I am not able to pass it a pointer to the member function of the class.
Can someone please point out my error. Thanks.
Your main problem is cross-posting.

The second problem is that a funcA has a hidden "this" pointer, which is not
passed through for you by pthread_create(). Use a "friend" wrapper, or declare
the funcA as void*funcA(void) and have the "this" pointer passed explicitly:

pthread_create(&threadId1, 0, (void (*)(void))this->funcA, this);

You'll lose the ability to pass &c or &d pointer, though.
class test
{
public:
void* funcA(void* dud)
{
int i=0;
char c,*d;
d=(char*)dud;
c=*d;
for(i=0;i<100;i++)
printf("%c",c);
}

int funcB()
{
pthread_t threadId1,threadId2;
char c,d;
c='a';
pthread_create(&threadId1,NULL,this->funcA,&c); //I keep getting
compilation errors for this line. I tried a::funcA, &a::funcA, funcA,
&funcA. None of them work.
d='b';
pthread_create(&threadId2,NULL,this->funcA,&d); //I keep getting
compilation errors for this line
pthread_join(threadId2,NULL);
pthread_join(threadId1,NULL);
perror("say what");
}
};

main()
{
test obj;
obj.funcB();
}

--
Lev Walkin
vl*@lionet.info
Jul 22 '05 #3
Lev Walkin <vl*@lionet.info> writes:
pthread_create(&threadId1, 0, (void (*)(void))this->funcA, this);


The result of doing the above is still undefined.

POSIX requires that an 'extern "C"' linkage function be passed to
pthread_create().

This has been discussed many times and at length in com.programming.threads

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David Hill | last post by:
Hello - I am using a library that takes a function pointer as an argument. Is the code below not possible? int library_func(void (*func)(int, short, void *)); I am trying to do this... ...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.