Member Functions as Thread Procs

This is a discussion on Member Functions as Thread Procs within the C++ Programming forums, part of the General Programming Boards category; Is there any way to specify a class member function as the thread procedure in calls such as beginthreadex() or ...

  1. #1
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186

    Member Functions as Thread Procs

    Is there any way to specify a class member function as the thread procedure in calls such as beginthreadex() or CreateThread()?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Member Functions as Thread Procs

    Originally posted by johnnie2
    Is there any way to specify a class member function as the thread procedure in calls such as beginthreadex() or CreateThread()?
    Yes, but they must be static member functions............

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    class foobar{
    public:
    	static DWORD WINAPI ThreadProc(LPVOID);
    };
    
    DWORD WINAPI foobar::ThreadProc(LPVOID lpv){
    	std::cout << static_cast<char*>(lpv);
    	return 0;
    }
    
    
    int main(void){
    
    	
    
    	DWORD dwDummy;
    	HANDLE hHand;
    	char str[] = "Hello World";
    
    	hHand = CreateThread(0,0,&foobar::ThreadProc,
    		static_cast<LPVOID>(str),0,&dwDummy);     
    		
    	WaitForSingleObject(hHand,INFINITE);
    
    };

  3. #3
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Thanks for the response. I'm guessing there is absolutely no way to accomplish this with non-static member functions?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    Right.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread safety for tiny simple functions
    By CodeMonkey in forum C++ Programming
    Replies: 16
    Last Post: 12-30-2008, 11:20 PM
  2. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  5. illegal references in static member functions
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 12-31-2002, 09:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21