Function Pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    Function Pointers

    Touchy subject I know, but I'm currently trying some stuff to understand Threads better in C++.

    I've read the Windows API documentation regarding Threads, and want to build a Thread class that can run any kind of function as a seperate thread by utilising function pointers.
    This is the code I currently have so far:

    header file
    Code:
    //---------------------------------------------------------------------------
    #ifndef ThreadH
    #define ThreadH
    #include <windows.h>
    
    /*
    	Windows Definitions of the types
    	DWORD = unsigned long
    	SIZE_T = unsigned int
    	LPVOID = void*
    */
    
    class Thread
    {
    	public:
    		Thread(int* FunctionAddress, int* ParamList);
    		~Thread();
    
    		bool Execute(bool Suspend);
    
    		void StartThread();
    		
    	protected:
        	static DWORD WINAPI ThreadMainFunction(void* Param);
    	private:
            bool mSuspended;
    		int* mFunctionAddress;
    		int* mParamList;
    		unsigned long* mThreadId;
    		HANDLE mMyThread;
    };
    //---------------------------------------------------------------------------
    #endif
    source file
    Code:
    //---------------------------------------------------------------------------
    #pragma hdrstop
    #include "Thread.h"
    
    // This function is the constructor of the thread.
    // It takes 2 arguments, both are a pointer.
    // FunctionAddress is a pointer to the function beeing
    // called.
    // Paramlist is a the data passed as func arg.
    Thread::Thread(int* FunctionAddress, int* ParamList)
    {
    	// Assign the variables to our internal datamembers
    	mFunctionAddress = FunctionAddress;
    	mParamList = ParamList;
    }
    //---------------------------------------------------------------------------
    Thread::~Thread()
    {}
    //---------------------------------------------------------------------------
    // This function creates the thread and starts it.
    bool Thread::Execute(bool Suspend = false)
    {
    	// Store if we are suspended or not.
    	mSuspended = Suspend;
    
    	// Create the Thread.
    	if(true == Suspend)
    	{
    		mMyThread = ::CreateThread(0, 0, ThreadMainFunction, this, CREATE_SUSPENDED, mThreadId);
    	}
    	else
    	{
    		mMyThread = ::CreateThread(0, 0, ThreadMainFunction, this, 0, mThreadId);
    	}
    
    	// Return the assing result of myThread.
    	return (0 != mMyThread);
    }
    //---------------------------------------------------------------------------
    // This function starts the thread if it hasn't started already.
    void Thread::StartThread()
    {
    	if((true == mSuspended) && (0 != mMyThread))
    	{
        	::ResumeThread(mMyThread);
        }
    }
    //---------------------------------------------------------------------------
    // This function is the real mainloop of the thread.
    // Once this function returns the tread is ended.
    // Because Windows only accepts functions in the format
    // DWORD WINAPI FuncProc(LPVOID param), we create this
    // signature and call the function ourselves from within
    // this function.
    // Function is declared static to prevent compiler errors about
    // accessing data.
    DWORD WINAPI Thread::ThreadMainFunction(void* Param)
    {
    	// Cast our object back to a Thread
    	Thread* myself = static_cast<Thread*>(Param);
    	
    	// Call the actuall function to be executed.
    	return (*(myself->mFunctionAddress)(myself->mParamList));
    }
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    Current output from compiler:
    [BCC32 Error] Thread.cpp(68): E2314 Call of nonfunction

    And that points to the following line:
    Code:
    return (*(myself->mFunctionAddress)(myself->mParamList));
    Bit clueless on the topic, so could someone explain it tome what I'm doing wrong?
  • Airslash
    New Member
    • Nov 2007
    • 221

    #2
    got the code compiled now, just testing needs to be done if what I wrote actually works....

    had to change the declaration of the void* to the function adress into void* (*mFunctionAddr ess)(void *)

    Comment

    Working...