We want to execute a list of tasks asynchronously from the main thread but retain their order relative to each other. Let's implement a queue for such tasks.
We need mutexes and smart pointers from the previous recipes to do this, since the queue needs synchronization primitives to keep its internal data structures consistent, and it needs smart pointers to prevent tasks from leaking.
The interface for tasks we want to put into the worker thread is as follows:
class iTask: public iObject { public: iTask() : FIsPendingExit(false) , FTaskID(0) , FPriority(0) {};
The
Run()
method contains a payload of our task. It is where all the useful work is done:virtual void Run() = 0;
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 flag, which means we want to exit:virtual void...