Пример #1
0
	unsigned int __stdcall ThreadPool::Execute(void* _args)
	{
		// TODO - implement your own version
		//return ExecuteSolution(_args);
		ThreadPool* self = (ThreadPool*)_args;

		bool run = true;					// local run bool for completeing last bit of work
		WorkerThread* workerPtr = nullptr;	//worker thread
		int work = 0;						// amount of work left in the queue

		while( run )
		{
			workerPtr = nullptr;
			self->GetWork( &workerPtr );
			if( workerPtr != nullptr )
			{
				workerPtr->DoWork();
				workerPtr->workDone.Post();
			}
			self->runMutex.Lock();
			run = self->run;
			self->runMutex.Unlock();

			self->queueMutex.Lock();
			work = self->workers.size();
			self->queueMutex.Unlock();

			if( work > 0 )
				run = true;
		}
		
		/*self->runMutex.Lock();
		if(self->run == false)
		{
			
			while( self->workers.size() > 0 )
			{
				WorkerThread* workerPtr = nullptr;
				self->GetWork( &workerPtr );
				if( workerPtr != nullptr )
				{
					workerPtr->DoWork();
					workerPtr->workDone.Post();
				}
				else
					break;
			}
		}
		self->runMutex.Unlock();*/

		return self->shutdownType;
	}