Thread: sending member function to function

  1. #1
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20

    sending C++ member function to C function

    I have a C++ member function that I need to send to a C library function, but it keeps yelling at me

    Here's what I do:

    pthread_create(&pthread_id, NULL, this->*_run, NULL);

    Here' s what it says:

    cannot convert 'void *(MyClass::*)(void*)' to 'void *(*)(void *)'

    Any ideas? Any help would be *greatly* appreciated.


    NOTE: There was a major edit to this post once I found the real problem
    Last edited by Joe Monti; 04-01-2003 at 02:24 PM.
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "void *(*_run) (void *data)" is C-function pointer and can't be used by non-static member functions.

    This thread may light up some bulbs.

    gg

  3. #3
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Thanks, I'll take a look at that and see what I can do.

    Also, can I just not bother sending the run function to start and just acess the sub-class implementation of run from the start implementation in the base class? Thats me hoping so I just have to call start() in the sub-class. But I doubt the reference to the right implementation of run will be sent in start even though run is virtual. Basically i'm wondering if theres a better way to get run to the pthread call.

    Also, would it be a problem sending a C++ member function to a C library function?
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  4. #4
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    I tried some ideas from that thread and I think I ran into the real problem: Sending C++ member function to C library function

    Any ideas?
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Without knowing exactly what you are trying to do its difficult to be any more than general. You can pass a forwarding function. This can be a free function or a static member function. In the forwarding function use pointer to member syntax to call your member function as per examples in quoted thread. Make sure your forwarding function has the expected type for the call to the library function.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I whipped this up - it should give you some ideas (or you can use this).

    Code:
    //thread function pointer data type
    typedef void (*thread_func_t)(void*);
    
    //Base thread class
    class BaseThread
    {
        //thread function that calls run() interface
        static void thread_func(void *thisp) {reinterpret_cast<BaseThread*>(thisp)->run();}
    public:
        //overloaded cast operator for thread function type
        operator thread_func_t() {return thread_func;}
    
        //overloaded cat operator for thread function data (void*)
        operator void*() {return reinterpret_cast<void*>(this);}
    
        //"run" interface for thread
        virtual void run() {cout << "BaseThread run" << endl;}
    };
    
    //Thread extension, implementing it's own run()
    class MyThread : public BaseThread
    {
    public:
        virtual void run() {cout << "MyThread run" << endl;}
    };
    
    //test "pthread_create" function
    void run(thread_func_t tf,void *data) {tf(data);}
    
    int main()
    {
        MyThread mt;
        BaseThread bt;
    
        run(bt,bt);
        
        run(mt,mt);
    
        return 0;
    }
    /*
    This will output:
    BaseThread run
    MyThread run
    */
    gg

  7. #7
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Thank you so much!

    It took me a while to figure out what exactly was going on, but I was able to mess around with it to get it working with my setup. That really helped me get back into C++ mode.

    now I just gotta figure out how to serialize an object
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM