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
Expand|Select|Wrap|Line Numbers
- //---------------------------------------------------------------------------
- #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
Expand|Select|Wrap|Line Numbers
- //---------------------------------------------------------------------------
- #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)
[BCC32 Error] Thread.cpp(68): E2314 Call of nonfunction
And that points to the following line:
Expand|Select|Wrap|Line Numbers
- return (*(myself->mFunctionAddress)(myself->mParamList));