Thread: Member Functions as Thread Procs

  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,793

    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,708
    Right.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(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-31-2008, 12:20 AM
  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, 10:11 AM