Пример #1
0
UINT __stdcall CThreadPool::_ThreadProc(LPVOID pParam)
#endif
{
	//BUGTRAP_THREAD_IN(_ThreadProc)

	DWORD					dwWait;
	CThreadPool*			pool;
  	HANDLE					hThread = GetCurrentThread();
	LPTHREAD_START_ROUTINE  proc;
	LPVOID					data;
	DWORD					dwThreadId = GetCurrentThreadId();
	HANDLE					hWaits[2];
	IRunObject*				runObject;
	bool					bAutoDelete;

	ASSERT(pParam != NULL);
	if(NULL == pParam)
	{
		//BUGTRAP_THREAD_OUT(_ThreadProc)
		return -1;
	}

	pool = static_cast<CThreadPool*>(pParam);
	hWaits[0] = pool->GetWaitHandle(dwThreadId);
	hWaits[1] = pool->GetShutdownHandle();
	
	loop_here:

	dwWait = WaitForMultipleObjects(2, hWaits, FALSE, INFINITE);

	if(dwWait - WAIT_OBJECT_0 == 0)
	{
		if(pool->CheckThreadStop())
		{
			//BUGTRAP_THREAD_OUT(_ThreadProc)
			return 0;
		}
		
		if(pool->GetThreadProc(dwThreadId, proc, &data, &runObject))
		{
			pool->BusyNotify(dwThreadId);
			
			if(proc == NULL)
			{
				runObject->Run();

				runObject->Release();
			}
			else
			{
				// note: the user's data is wrapped inside UserPoolData object
				proc(data);
				
				// Note: data is created by the pool and is deleted by the pool.
				//       The internal user data is not deleted by the pool.
				UserPoolData* pPoolData = static_cast<UserPoolData*>(data);
				delete pPoolData;	
			}
		}
		else
		{
			pool->FinishNotify(dwThreadId); // tell the pool, i am now free
		}

		goto loop_here;
	}
			
	//BUGTRAP_THREAD_OUT(_ThreadProc)
	return 0;
}
Пример #2
0
UINT __stdcall CThreadPool::_ThreadProc(LPVOID pParam)
#endif
{
	DWORD					dwWait;
	CThreadPool*			pool;
  	HANDLE					hThread = GetCurrentThread();
	LPTHREAD_START_ROUTINE  proc;
	LPVOID					data;
	DWORD					dwThreadId = GetCurrentThreadId();
	HANDLE					hWaits[2];
	IRunObject*				runObject;
	bool					bAutoDelete;

	if(NULL == pParam)
	{
		return -1;
	}

	pool = static_cast<CThreadPool*>(pParam);
	hWaits[0] = pool->GetWaitHandle(dwThreadId);
	hWaits[1] = pool->GetShutdownHandle();
	
	loop_here:

	dwWait = WaitForMultipleObjects(2, hWaits, FALSE, INFINITE);

	if(dwWait - WAIT_OBJECT_0 == 0)
	{
		if(pool->CheckThreadStop())
		{
			return 0;
		}

		// a new function was added, go and get it
		if(pool->GetThreadProc(dwThreadId, proc, &data, &runObject))
		{	
			pool->BusyNotify(dwThreadId);
			
			if(proc == NULL)
			{
				// a function object is being used instead of 
				// a function pointer.
				bAutoDelete = runObject->AutoDelete();
				
				runObject->Run();

				// see if we need to free this object
				if(bAutoDelete)
				{
					delete runObject;
				}
			}
			else
			{
				// note: the user's data is wrapped inside UserPoolData object
				proc(data);
				
				// Note: data is created by the pool and is deleted by the pool.
				//       The internal user data is not deleted by the pool.
				UserPoolData* pPoolData = static_cast<UserPoolData*>(data);
				delete pPoolData;	
			}

			pool->FinishNotify(dwThreadId); // tell the pool, i am now free
		}

		goto loop_here;
	}
			
	return 0;
}