libosmscout 0.1
Loading...
Searching...
No Matches
osmscout::AsyncWorker Class Reference

Async worker provides simple tool for providing asynchronous method calls. More...

#include <AsyncWorker.h>

Public Member Functions

 AsyncWorker (AsyncWorker &&)=delete
 
 AsyncWorker (const AsyncWorker &)=delete
 
 AsyncWorker (const std::string &name)
 
void DeleteLater ()
 
std::thread::id GetThreadId () const
 
void Loop ()
 
AsyncWorker & operator= (AsyncWorker &&)=delete
 
AsyncWorker & operator= (const AsyncWorker &)=delete
 
void 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.
 
void ThreadAssert () const
 
virtual ~AsyncWorker ()
 

Protected Member Functions

template<typename T >
CancelableFuture< T > Async (const std::function< T(Breaker &)> &task)
 

Detailed Description

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
{
Stop();
}
void Stop()
Stops the worker: it breaks the breakers of the jobs that are in flight, stops the queue,...
Definition AsyncWorker.cpp:66

Constructor & Destructor Documentation

◆ AsyncWorker() [1/3]

osmscout::AsyncWorker::AsyncWorker ( const std::string &  name)
explicit

References name, and osmscout::SetThreadName().

◆ ~AsyncWorker()

osmscout::AsyncWorker::~AsyncWorker ( )
virtual

◆ AsyncWorker() [2/3]

osmscout::AsyncWorker::AsyncWorker ( const AsyncWorker &  )
delete

◆ AsyncWorker() [3/3]

osmscout::AsyncWorker::AsyncWorker ( AsyncWorker &&  )
delete

Member Function Documentation

◆ Async()

◆ DeleteLater()

void osmscout::AsyncWorker::DeleteLater ( )

References Stop().

◆ GetThreadId()

std::thread::id osmscout::AsyncWorker::GetThreadId ( ) const
inline

◆ Loop()

◆ operator=() [1/2]

AsyncWorker & osmscout::AsyncWorker::operator= ( AsyncWorker &&  )
delete

◆ operator=() [2/2]

AsyncWorker & osmscout::AsyncWorker::operator= ( const AsyncWorker &  )
delete

◆ Stop()

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().

◆ ThreadAssert()

void osmscout::AsyncWorker::ThreadAssert ( ) const
inline

The documentation for this class was generated from the following files: