Thread



  • Hallo, ich arbeite grad mit Thread nun will ich die ThreadFunction in eine Klasse geben...
    warum brauch ich da diesen cast:

    MyThreadClass *ptrMyThreadClass = static_cast<MyThreadClass*>(pParam);
    

    weil man da den void *pParam Pointer auf einen Pointer der MyThreadClass casten muss um die Methode aufzurufen...ich kann das nicht so gut erklären*g*
    _beginthread will als ersten Parameter einen Funktionspointer? nun muss ja wenn ich den umweg über diese ThreadHelpFunction nicht gehen will, die methode WorkFunction static sein, damit ich die direkt _beginthread übergeben kann?

    greets wolf

    #include <process.h>
    #include <windows.h>
    
    class MyThreadClass
    {
    public:
    	void WorkFunction();
    };
    
    void MyThreadClass::WorkFunction()
    {
    	cout << "Thread started" << endl;
    	Sleep(1000);
    }
    
    void ThreadHelpFunction(void *pParam)
    {
    	MyThreadClass *ptrMyThreadClass = static_cast<MyThreadClass*>(pParam);
    	ptrMyThreadClass->WorkFunction();
    	_endthread();
    }
    
    void main(void)
    {
            MyThreadClass mt;
    
    	_beginthread(ThreadHelpFunction,			// Pointer to Thread Function
    		     0,					// Stack Size set automatically
    		     &mt);				// Parameter: Pointer to Instance
    
    	Sleep(5000);
    
    }
    


  • hallo, wäre das so ok?

    cu

    class MyThreadClass
    {
    public:
    	static void WorkFunction(void *pParam);
    };
    
    void MyThreadClass::WorkFunction(void *pParam)
    {
    	cout << "Thread started" << endl;
    	Sleep(1000);
    	_endthread();
    }
    
    int main()
    {
    
    	        MyThreadClass mt;
    
    	_beginthread(mt.WorkFunction,			// Pointer to Thread Function
    		     0,					// Stack Size set automatically
    		     &mt);				// Parameter: Pointer to Instance
    
    return 0;
    }
    

Anmelden zum Antworten