Game Development Reference
In-Depth Information
3.
A task cannot be safely terminated from outside, since the foreign code does not
know the current state of the task and what kind of work it is doing now. So, the
Exit() method just sets an appropriate lag, which means we want to exit:
virtual void Exit() { FIsPendingExit = true; }
4. We can check this lag inside the Run() method by calling IsPendingExit() :
virtual bool IsPendingExit() const volatile
{
return FIsPendingExit; }
5.
Tasks should be distinguishable from each other. That is what IDs are for:
virtual void SetTaskID( size_t ID ) { FTaskID = ID; };
virtual size_t GetTaskID() const { return FTaskID; };
private:
volatile bool FIsPendingExit;
size_t FTaskID;
};
6.
And here, is the interface of the worker thread (the complete implementation
can be found in the topic's download pack):
class WorkerThread: public iThread
{
public:
7.
We can enqueue and cancel tasks at will:
virtual void AddTask( const clPtr<iTask>& Task );
virtual bool CancelTask( size_t ID );
virtual void CancelAll();
8.
The ExtractTask() private method is used to access the list of tasks atomically:
private:
clPtr<iTask> ExtractTask();
clPtr<iTask> FCurrentTask;
private:
std::list< clPtr<iTask> > FPendingTasks;
tthread::mutex FTasksMutex;
tthread::condition_variable FCondition;
};
 
Search WWH ::




Custom Search