LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-28-2003, 02:08 AM   #1
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
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

Last edited by champ; 03-28-2003 at 05:45 AM.
 
Old 03-28-2003, 03:48 AM   #2
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
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.

Last edited by GtkUser; 03-28-2003 at 03:50 AM.
 
Old 03-28-2003, 06:22 PM   #3
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Original Poster
Rep: Reputation: 16
thanks, it worked great using a static method
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C Programming: Help with Pointers to Functions devinWhalen Programming 2 03-04-2005 01:00 PM
Accessing member functions of sockaddr_in Stack Overflow Programming 4 01-27-2005 05:11 PM
c to GCChange of member functions in iostream from GCC296 to Gcc 3.2.2 Basiltp Programming 0 11-29-2004 03:19 PM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 07:45 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM


All times are GMT -5. The time now is 10:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration