Async worker provides simple tool for providing asynchronous method calls.
Functions executed via Async method are executed in contex of worker thread. If all class fields are modified in context of worker thread, there is no need of synchronisation.
Ownership rule: a job runs against the object it was submitted on, so the object must stop its worker before its own state is destroyed. A class that derives from AsyncWorker and runs jobs that touch its members therefore calls Stop() at the beginning of its destructor, because its members are destroyed before the base class destructor runs:
~MyWorker() override
{
}
void Stop()
Stops the worker: it breaks the breakers of the jobs that are in flight, stops the queue,...
Definition AsyncWorker.cpp:66
| void osmscout::AsyncWorker::Stop |
( |
| ) |
|
Stops the worker: it breaks the breakers of the jobs that are in flight, stops the queue, discards the jobs that have not started, and then waits for the job that is running, so that no job runs against state that is gone.
It is harmless to call this more than once.
A derived class whose jobs read or write its members must call this at the beginning of its own destructor: the base class is destroyed after the derived members, so the worker thread would otherwise keep running a job against state that is already gone.
A job that polls the Breaker it received stops at its next check, which bounds the wait; a job that does not poll is still waited for, so correctness does not depend on a job cooperating. When the call comes from the worker thread itself there is nothing to wait for, and the destructor disposes of the thread - which also keeps GetThreadId() usable for a derived destructor.
References osmscout::ProcessingQueue< T >::StopAndDiscard().
Referenced by DeleteLater(), and ~AsyncWorker().