Skip to main content
GameDev.net gamedev.net
🔒 Locked

Thread interface

Started by sprinter_trueno Feb 24, 2003 at 6:03 AM 0 replies 200+ views
Original Post
sprinter_trueno
sprinter_trueno
Hi all, I try to focus on inheritance n object composition recently but I must say that it still gives me quite a headache since I never really know whether my ideas are effective at all (like someone would come around n say ''Works, but is a complete waste of time'' or sth. like that). I wanna create a Thread interface which gives a subclass the possibility to produce objects that can run in their own threads. So far I had the following idea.
  

class CThread
{
public:

	//////////////////////////////

	// Constructor / Destructor //

	//////////////////////////////

	CThread(); virtual ~CThread();

	/////////////////////////

	// The thread function //

	/////////////////////////

	static DWORD __stdcall ThreadRoutine(LPVOID lparam)
	{
              ///////////////////////////////

              // Get the threadable object //

              ///////////////////////////////

              CThread* pThreadable = (CThread*)lparam;

              //////////////////////////////////////////

              // Execute it''s defined thread function //

              //////////////////////////////////////////

              pThreadable->ThreadWork();
        }
	

	////////////////////////////////////////////

	// The functions to manipulate the thread //

	////////////////////////////////////////////

	virtual void Start(CThread* Threadable)
	{
		// Start the thread

		ThreadHandle = CreateThread(NULL,NULL,ThreadRoutine,Threadable,NULL,NULL);
	};

	virtual void Stop()  = 0;
	virtual void Pause() = 0;

        //////////////////////////////////////////////////////

        // The function that will be executed in the thread //

        //////////////////////////////////////////////////////

        virtual void ThreadWork()  = 0;

        ///////////////////////////////

        // Keeping the thread handle //

        ///////////////////////////////

	HANDLE ThreadHandle;
};

  
The ThreadWork function is the key type in this interface and will be called in the base class. The example for a subclass would look like this:
  

class CTest : private CThread
{
public:

	//////////////////////////////

	// Constructor / Destructor //

	//////////////////////////////

	CTest(); virtual ~CTest();

	////////////////////////////////////////////

	// The functions to manipulate the thread //

	////////////////////////////////////////////

	virtual void Start() { CThread::Start(this) };
	virtual void Stop()  { CThread::Stop()      };
	virtual void Pause() { CThread::Pause()     };

	////////////////////////////////////////////////////////////////////////////////

	// The function which defines the task, an object of this class would perform //

	////////////////////////////////////////////////////////////////////////////////

	virtual void ThreadWork()  { /* .... */ };

};

  
That''s the idea so far. Is this crap ? If yes, what would be a better approach ? Don''t mention any coding mistakes. I don''t have a compiler here to check. ( NotePad rulez ) Thanks for any comments n teachings. Lorenz

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.