Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: About Thread argument Pin
Cedric Moonen14-Dec-07 0:53
Cedric Moonen14-Dec-07 0:53 
GeneralRe: About Thread argument Pin
CPallini14-Dec-07 1:09
mveCPallini14-Dec-07 1:09 
GeneralRe: About Thread argument Pin
Cedric Moonen14-Dec-07 1:15
Cedric Moonen14-Dec-07 1:15 
GeneralRe: About Thread argument Pin
CPallini14-Dec-07 2:43
mveCPallini14-Dec-07 2:43 
GeneralRe: About Thread argument Pin
Cedric Moonen14-Dec-07 1:12
Cedric Moonen14-Dec-07 1:12 
GeneralRe: About Thread argument Pin
CPallini14-Dec-07 2:50
mveCPallini14-Dec-07 2:50 
JokeRe: About Thread argument Pin
Cedric Moonen14-Dec-07 4:06
Cedric Moonen14-Dec-07 4:06 
GeneralRe: About Thread argument Pin
Member 75496014-Dec-07 9:08
Member 75496014-Dec-07 9:08 
Thread management is not trivial. You need to have a clear understanding of what is involved in order to use them effectively. Simply using a class pointer as the thread argument can lead to trouble; as has been mentioned, consider,
class gladiator
{
	gladiator() {}
	virtual ~gladiator()
	{
		// moriuntur te salute
		_beginthread(&threadFunction, 0, this);
	}

	static void threadFunction(void *pargList)
	{
		if (!pargList)
			return;

		gladiator * pActor = static_cast<gladiator *>(pargList);
		
		// pActor dead or about to be; using it will be deadly.

		_endthread();
	}
};

This highlights that the owner of a thread needs to manage the thread lifetime. There are numerous ways to do this but if you use the class object then the class should hold on to the handle returned by _beginthread and insure proper thread termination before object destruction.
class gladiator
{
	gladiator()
	:	m_bThreadAlive(false),
		m_bStopThread(false),
		m_hThread(-1)
	{
		m_hThread = _beginthread(&threadFunction, 0, this);
		// check for error return value (-1)
	}
	virtual ~gladiator()
	{
		shutdown();
	}

protected:
	bool m_bThreadAlive; 
	bool m_bStopThread; 
	HANDLE m_hThread;

	virtual void shutdown()
	{
		if (m_bThreadAlive) {
			do {
				m_bStopThread = true;
			} while (m_bThreadAlive);
		}
	}

	static void threadFunction(void *pargList)
	{
		if (!pargList)
			return;

		gladiator * pActor = static_cast<gladiator *>(pargList);
		pActor->m_bThreadAlive = true;
		
		while (!pActor->m_bStopThread) {
			// fight
		}

		pActor->m_bThreadAlive = false;
		_endthread();
	}
}; 

Note, the destructor for the gladiator class has a potentially endless while loop. A bug in the threadFunction while loop may prevent it from exiting properly. This is why the thread handle is needed to force the thread to exit if it fails to respond to the request to terminate (m_bStopThread). You can use whatever condition you want in shutdown() but it has the final say on the thread lifetime,
void shutdown()
{
	if (m_bThreadAlive) {
		m_bStopThread = true;
		long lCoundown = 10000L;
		do {
			if (--lCoundown < 1L) {
				if (!TerminateThread(m_hThread, -1)) {
					// throw an exception
				}
				m_bThreadAlive = false;
			}
		} while (m_bThreadAlive);
	}
}

Threads have potential problems with concurrent data access as well (see EnterCriticalSection, etc.) and you probably want to look at using events (see CreateEvent, etc.) to communicate.
Generalcheck if service is available - Vista Pin
Lord_Draconis13-Dec-07 22:17
Lord_Draconis13-Dec-07 22:17 
GeneralRe: check if service is available - Vista Pin
JudyL_MD14-Dec-07 2:24
JudyL_MD14-Dec-07 2:24 
GeneralRe: check if service is available - Vista Pin
Lord_Draconis14-Dec-07 3:41
Lord_Draconis14-Dec-07 3:41 
GeneralRe: check if service is available - Vista Pin
JudyL_MD14-Dec-07 3:53
JudyL_MD14-Dec-07 3:53 
QuestionPrint Preview in Dialog Pin
Shivarudrayya H13-Dec-07 21:57
Shivarudrayya H13-Dec-07 21:57 
GeneralRe: Print Preview in Dialog Pin
Nelek13-Dec-07 22:38
protectorNelek13-Dec-07 22:38 
GeneralRe: Print Preview in Dialog Pin
Shivarudrayya H13-Dec-07 22:56
Shivarudrayya H13-Dec-07 22:56 
GeneralRe: Print Preview in Dialog Pin
Nelek13-Dec-07 23:57
protectorNelek13-Dec-07 23:57 
GeneralRe: Print Preview in Dialog Pin
santhoshv8413-Dec-07 22:39
santhoshv8413-Dec-07 22:39 
Generalc++ and ... Pin
mostafa_h13-Dec-07 21:49
mostafa_h13-Dec-07 21:49 
GeneralRe: c++ and ... Pin
Nelek13-Dec-07 22:27
protectorNelek13-Dec-07 22:27 
GeneralRe: c++ and ... Pin
mostafa_h13-Dec-07 23:15
mostafa_h13-Dec-07 23:15 
GeneralRe: c++ and ... Pin
toxcct13-Dec-07 23:40
toxcct13-Dec-07 23:40 
GeneralRe: c++ and ... [modified] Pin
Nelek14-Dec-07 0:09
protectorNelek14-Dec-07 0:09 
GeneralRe: c++ and ... Pin
toxcct14-Dec-07 0:40
toxcct14-Dec-07 0:40 
JokeRe: c++ and ... Pin
Nelek14-Dec-07 0:42
protectorNelek14-Dec-07 0:42 
GeneralRe: c++ and ... Pin
toxcct14-Dec-07 0:47
toxcct14-Dec-07 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.