Threads?

Started by
3 comments, last by JwayneT 22 years, 12 months ago
How do threads in windows work?
Say I wanted to put a method of a class in a thread, is that possible? If so then how.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
Advertisement
Do I look like your F1 key? Search or grep your docs before coming to the board. Searching for "thread" should pull up enough to get you started.
Actually, what you are asking is, how do threads work in C++?

Well, all of the [current] standard thread creation functions (e.g., _beginthread(), CreateThread()) are built around C.

So, if you want to use them for C++ methods, you'd have to do one of two things:

1) Make the C++ method static. This means that the method could not access member variables. A static member function works within the C Thread libraries, because there is no implicit _this_ argument.
2) If you want to run a non-static method in a seperate thread, you'd have to use a much more complex method...I created my own class for this (based on what I had read elsewhere).

Here's the main body of my code...
    /*Rob Duckles, 200012/30/00MemberThread allows you to call class methods in seperate threadswhile retaining the ability to use member variables of the class.Problem:Thread creation is built around C-style functions;There is no way to execute class method as a seperate threadunless defined as static.  Static methods do not have access to member variables.This is because normal methods have an implicit first argument: the _this_ pointer.Usage:#include <process.h>...TestClass testclass;...MemberThread<TestClass> memberthread;memberthread.pObj = &testclassmemberthread.pFn  = &TestClass::FunctionToCall;memberthread.BeginThread();Or just:memberthread.BeginThread(&testclass, &TestClass::FunctionToCall);*/template <class ObjType> class MemberThread  {public:	ObjType *pObj;			//pointer to class instance (instead of an implicit _this_ ptr)	void (ObjType::*pFn)();	//pointer to the function to be called	MemberThread(): pObj(NULL), pFn(NULL) {} //initializationprivate:	//structure passed to thread	struct ThreadArgs_t	{		ObjType *p;				//pointer to object 		void(ObjType::*pmf)();	//pointer to member function		//constructor		ThreadArgs_t(ObjType *pObj, void(ObjType::*pFn)()): p(pObj), pmf(pFn){} 	};public:	BOOL BeginThread()	{		if ((pObj == NULL) || (pFn == NULL))		{			return FALSE;	//exit if variables haven't been set		}		//init args to pass to thread		ThreadArgs_t* pThreadArgs_s;		if (NULL == (pThreadArgs_s = new ThreadArgs_t(pObj, pFn)))		{			return FALSE;		}		DWORD thread_id;		//begin thread with proper args, sent to intermediary function		if (NULL == CreateThread(NULL, 0, MemberThread::intermediary, pThreadArgs_s, 			0, &thread_id)) 		{			//if failed, cleanup			delete pThreadArgs_s;			return FALSE;		}		return TRUE;	}	//in case you want to do it all in one call	BOOL BeginThread(ObjType *p, void(ObjType::*pmf)())	{		pObj = p;		pFn  = pmf;		return BeginThread();	}private:	//does internal work	static DWORD WINAPI intermediary(void *ptr)	{		if (!ptr)		{			ExitThread(0);		}		//retrieve args		ThreadArgs_t *pThreadArgs_s = (ThreadArgs_t *)ptr;		//retrieve pointer to class object		ObjType* pObj = pThreadArgs_s->p;		//retrieve pointer to function		void (ObjType::*pFn)() = pThreadArgs_s->pmf;		//cleanup		delete pThreadArgs_s;		if ((!pObj) || (!pFn))		{			ExitThread(0);		}		//call member function		(pObj->*pFn)();		//thus the thread ends		ExitThread(0);	}};    


That should help you with using threads in C++.


Edited by - Dragn on April 23, 2001 9:55:23 PM
I've got a thread class too. It follows the Java Thread interface, but it's in C++, written for Windows.

www.eecs.ukans.edu/~millew

Questions and comments welcome.


Edited by - WMiller on April 23, 2001 12:42:58 AM
Much thankx guys, that''s exactly what I needed!
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]

This topic is closed to new replies.

Advertisement