Game Development Reference
In-Depth Information
How it works...
We start a single worker thread and run a simple task. The key difference from running three
separate threads is that all the tasks are executed sequentially and a common resource,
which is the output window in our case, is also used sequentially without the need for
handling concurrent access:
class TestTask: public iTask
{
public:
virtual void Run()
{
printf("Test\n");
}
};
int main()
{
WorkerThread* wt = new WorkerThread();
wt->Start( iThread::Priority_Normal );
Add three tasks one by one:
wt->AddTask( new TestTask() );
wt->AddTask( new TestTask() );
wt->AddTask( new TestTask() );
Tasks are never executed in parallel, only sequentially. Use a simple spinlock to wait for
completion of all tasks:
while (wt->GetQueueSize() > 0) {}
return 0;
}
Handling asynchronous callbacks invocation
One simple situation we may encounter in multithreaded programming is when we need
to run a method on another thread. For example, when a download task completes on a
worker thread, the main thread may want to be notiied of the task completion, to parse the
downloaded data. In this recipe we will implement a mechanism for such notiications.
 
Search WWH ::




Custom Search