Thread: void pointer to a member function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    void pointer to a member function

    Im trying to use createthread on a member function of an object and need a little help.

    CreateThread(NULL, 0, WHAT_GOES_HERE, (void *)null, 0, 1);

    And just a bs member function of myclass:

    DWORD WINAPI myclass::go(LPVOID nothing)

    I guess what i need is a void pointer to a member function of an object and i dont know how to get that. i tried playing around with "this->go" and the like, but im dumb. Also, dont complain about the void * to null. i dont want to pass any data, and im aware of how to properly use the createthread function, im just trying to get a really weird idea to work.

    thanks.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    Pointer to member functions won't work the way you expect. You can stuff them into a (void*), but you won't be able to call them without an instance of the class.

    This is an example I've used before to show how a pointer to a member function works:

    Code:
    #include <iostream>
    
    class CUtility
    {
    public:
      void setNum(int i) { num = i; }
      int getNum() { return num; }
    private:
      int num;
    };
    
    typedef void (CUtility::*pointerToMember)(int);
    
    int main()
    {
      pointerToMember func = &CUtility::setNum;
      CUtility utility;
    
      (utility.*func)(10);
    
      std::cout << utility.getNum() << std::endl;
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    CreateThreads awaits a function pointer,not a member function pointer.
    Function pointer and member function pointer are different beasts,there is no guarantee that sizeof(function pointer) == sizeof(member function pointer).

    You have to wrap the call to the member function into a static member function and pass a pointer to the latter.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    how do i go about doing that.
    2 member functions.
    first creates a thread of the second.
    what do i need to do in the first to create a new thread of the second. please provide an example or something because im lost on how to do this.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    Quick 'n dirty.
    I don't know the exact prototyp for the LPTHREAD_START_ROUTINE,maybe my version is wrong.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    class Foo
    {
    public:
    	static void StaticFooThreadFun(void *param) 
    	{
    		Foo *thisFoo = static_cast<Foo*>(param);
    		thisFoo->FooThreadFun();
    	}
    
    	void FooThreadFun() 
    	{
    		while(true)
    		{
    			cout << '.';
    			Sleep(100);
    		}
    	}
    };
    
    int main()
    {
    	Foo foo;
    	CreateThread(0,0,
    		(LPTHREAD_START_ROUTINE)Foo::StaticFooThreadFun,
    		&foo,0,0);
    	getch();
    }

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You cannot use a nonstatic member function pointer where you need a normal function pointer. Every nonstatic member function has a hidden parameter, the "this" pointer of the object. Because the function truly needs another hidden parameter, you can never use it where a normal function pointer is needed.

    Static member functions and normal functions can be used interchangeably; neither takes any hidden parameters, and you can pass a static member function (with the appropriate signature) to anything expecting a function pointer. WLedge shows a workaround to this; using a static function that takes a pointer, you can call a nonstatic member function on the pointer.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    you didnt understand: i need f1 to CREATE THE THREAD of f2 itself.


    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    class Foo
    {
    public:
    	static void StaticFooThreadFun(void *param) 
    	{
    		// Do Some Stuff...
    	}
    
    	void FooThreadFun() 
    	{
    		CreateThread(0,0, (LPTHREAD_START_ROUTINE)THIS_PARTICULAR_FOO::StaticFooThreadFun,&foo,0,0);
    	}
    };
    
    int main()
    {
    	Foo foo;
    	foo.FooThreadFun();
    	getch();
    }
    I dont know how to craft the CreateThread function so THIS would work.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    Originally posted by DeepBlackMagic
    I dont know how to craft the CreateThread function so THIS would work.
    That's because it can't. You can't call a static member function on a particular instance of a class. That's exactly the opposite of what a static function is designed to do. Instead, you have to use a static function as a wrapper to your member function - to do this, you'll need to pass the instance of the class as the parameter to CreateThread() (and therefore, into your static function).

    Basically.. you can't create a thread from a member function.. but you can call a member function from your thread.

    Wledge gave you the exact code that you need. It seems like you're simply complaining about the CreateThread() in main. You can easily toss that into a member function:

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    class Foo
    {
    public:
                    void RunFooThread()
                    {
                             CreateThread(0,
                                                    0,
                                                    (LPTHREAD_START_ROUTINE)StaticFooThreadFun,
                                                    this,
                                                    0,
                                                    0);
                    }
    
    
    	static void StaticFooThreadFun(void *param) 
    	{
    		Foo *thisFoo = static_cast<Foo*>(param);
    		thisFoo->FooThreadFun();
    	}
    
    	void FooThreadFun() 
    	{
    		while(true)
    		{
    			cout << '.';
    			Sleep(100);
    		}
    	}
    };
    
    int main()
    {
    	Foo foo;
                    foo.RunFooThread();
    	getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM