Ejemplo n.º 1
0
KOMODIA_NAMESPACE_START

#define CWin32Thread_Class "CWin32Thread"

CWin32Thread::CWin32Thread(LPGenericThreadProc pThreadProc) : CGenericThread(pThreadProc),
															  m_hThread(0)
{
	try
	{
		//Set our name
		SetName(CWin32Thread_Class);

		//Create the thread
		if (GetThreadProc())
		{
			//Our thread ID
			DWORD dwThreadID;

			//Create the thread in suspend mode
			m_hThread=CreateThread(NULL,
   								   0,
								   Win32Thread,
								   this,
								   CREATE_SUSPENDED,
								   &dwThreadID);

			//Check if created
			if (m_hThread)
			{
				//Set the thread ID
				SetThreadID(dwThreadID);

				//Set the status
				SetThreadStatus(tsSuspended);
			}
			else
				//An error
				throw std::string("Failed to create thread!");
		}
		else
			//Throw the error
			throw std::string("No thread proc!");
	}
	ERROR_HANDLER_RETHROW("CWin32Thread")
}
Ejemplo n.º 2
0
bool CThread::Start( unsigned nBytesStack )
{
	AUTO_LOCK( m_Lock );

	if ( IsAlive() )
	{
		AssertMsg( 0, "Tried to create a thread that has already been created!" );
		return false;
	}

	bool         bInitSuccess = false;

#ifdef _WIN32
	HANDLE       hThread;
	CThreadEvent createComplete;
	ThreadInit_t init = { this, &createComplete, &bInitSuccess };
	m_hThread = hThread = (HANDLE)CreateThread( NULL,
														nBytesStack,
														(LPTHREAD_START_ROUTINE)GetThreadProc(),
														new ThreadInit_t(init),
														0,
														&m_threadId );
	if ( !hThread )
	{
		AssertMsg1( 0, "Failed to create thread (error 0x%x)", GetLastError() );
		return false;
	}
#elif _LINUX
	ThreadInit_t init = { this, &bInitSuccess };
	pthread_attr_t attr;
	pthread_attr_init( &attr );
	pthread_attr_setstacksize( &attr, max( nBytesStack, 1024*1024 ) );
	if ( pthread_create( &m_threadId, &attr, GetThreadProc(), new ThreadInit_t( init ) ) != 0 )
	{
		AssertMsg1( 0, "Failed to create thread (error 0x%x)", GetLastError() );
		return false;
	}
	bInitSuccess = true;
#endif


#ifdef _WIN32
	if ( !WaitForCreateComplete( &createComplete ) )
	{
		Msg( "Thread failed to initialize\n" );
		CloseHandle( m_hThread );
		m_hThread = NULL;
		return false;
	}
#endif

	if ( !bInitSuccess )
	{
		Msg( "Thread failed to initialize\n" );
#ifdef _WIN32
		CloseHandle( m_hThread );
		m_hThread = NULL;
#elif _LINUX
		m_threadId = 0;
#endif
		return false;
	}

#ifdef _WIN32
	if ( !m_hThread )
	{
		Msg( "Thread exited immediately\n" );
	}
#endif

#ifdef _WIN32
	return !!m_hThread;
#elif _LINUX
	return !!m_threadId;
#endif
}