Background thread just... vanishes

Started by
1 comment, last by CaptainDeathbeard 14 years, 5 months ago
I have a background thread running in my game, and my problem is it just vanishes after the first few seconds. I'm on a dual core windows XP machine.

class ThreadClass
{
protected:
	HANDLE  threadHandle;
	DWORD   threadID;

public:
	ThreadClass() : threadHandle(NULL), threadID(0) {	}
	virtual ~ThreadClass() { end(); }

	void begin()
	{
		if( threadHandle )
			end();  // just to be safe.

		// Start the thread.
		threadHandle = CreateThread( NULL,
			0,
			(LPTHREAD_START_ROUTINE)cThreadProc,
			this,
			0,
			(LPDWORD)&threadID );
		
		if( threadHandle == NULL )		{
			// Arrooga! Dive, dive!  And deal with the error, too!
		}

//		SetThreadPriority(threadHandle, THREAD_PRIORITY_LOWEST);
		SetThreadPriority(threadHandle, THREAD_PRIORITY_BELOW_NORMAL);
//		SetThreadPriority(threadHandle, THREAD_PRIORITY_NORMAL);
	}


	void end()
	{
		//----------a breakpoint here never gets called
		if( threadHandle != NULL )
		{
			WaitForSingleObject( threadHandle, INFINITE );
			CloseHandle( threadHandle );
			threadHandle = NULL;
		}
	}

	virtual DWORD threadProc() = 0;
};

static DWORD WINAPI cThreadProc( ThreadClass* pThis ) {
	return pThis->threadProc();
}

and then a basic class inherits from it :

class ThreadGrouper : public ThreadClass
{
//stuff here
public:
	virtual DWORD threadProc()
	{
		while(1)
		{
		//mainloop stuff
		}		
		//--------a breakpoint here never gets called
		return 0;
	}
}
The stuff in the while loop runs for a bit at startup, then its just gone and putting a breakpoint in gets nothing. the end() function never gets called, so I have no idea what is happening, or how to debug it. Could anyone point me in the right direction?
Advertisement
If you break to the debugger, what does the Threads window show? That'll allow you to jump to the other thread and see what it's doing.
Ah thanks, I am still new to threading and never noticed that thread window before. It seems it deadlocked itself. I think I can fix it now, thanks.

This topic is closed to new replies.

Advertisement