LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   pointers to functions/member functions (http://www.linuxquestions.org/questions/programming-9/pointers-to-functions-member-functions-52147/)

champ 03-28-2003 02:08 AM

pointers to functions/member functions
 
Hello guys

I am at the moment writing a multithreaded server (what kind of server, is not relevant) using the pthread library.

I want to encapsulate some of the c code into c++ classes and instead of using a ordinary function as I would usually use as the running thread function, I want to use a member function.

Here comes my problem. When you want to spawn new threads you usually just do something like this:

Code:

int thread_func(int *)
{
// thread code goes here
}

int main()
{
pthread_t thread

pthread_create(&thread, NULL, (void * (*) (void)) thread_func, (void *)0);
}

...and this compiles with no problems. The problem comes when i want to do the same within a class.

Code:

// class implementation

void* HandlerThread::thread_func(void *data)
{
// thread code goes here
}

void HandlerThread::start()
{
pthread_create(&thread, NULL, (void * (*) (void)) thread_func, (void *)0);
}

...this does not work. Here's the compiler output:

handler.cpp: In method `void HandlerThread::start()':
handler.cpp:51: no matches converting function `thread_func' to type `void * (*)(void *)'
handler.cpp:43: candidates are: void * HandlerThread::thread_func(void *)


The pthread_create function takes as its third parameter a pointer to a function that takes a void pointer and returns a void pointer.

There is problem casting to a pointer to function when I really got
a member function. Maybe this is not possible. If not, I hope someone have any suggestions


Hope you understood my problem

GtkUser 03-28-2003 03:48 AM

The member functions that you are pointing to need to be static methods, or else global functions, or else you need to encasulate the object being used inside an adapter because local class methods require the object instance.

champ 03-28-2003 06:22 PM

thanks, it worked great using a static method


All times are GMT -5. The time now is 01:53 AM.