auto clinstance = myClass::create(); x::start_threadmsgdispatcher(clinstance);
x::start_threadmsgdispatcher
must be used to start an execution thread for a subclass of
x::threadmsgdispatcherObj
.
x::start_threadmsgdispatcher
() is a wrapper for
x::run().
The first parameter to the execution thread's
run
() message is a reference to an opaque
mcguffin. Any remaining parameters
to x::start_threadmsgdispatcher
() get forwarded as additional
parameters to the thread's
run
() method:
void run(x::ptr<x::obj> &threadmsgdispatcher_mcguffin) { msgqueue_auto msgqueue(this); threadmsgdispatcher_mcguffin=nullptr; try { while (1) msgqueue.event(); } catch (const x::stopexception &) { } catch (const x::exception &e) { LOG_ERROR(e); LOG_TRACE(e->backtrace); } }
The execution thread's
run
() method takes the following actions:
Constructs an instance of the
msgqueue_auto
class in automatic scope.
msgqueue_auto
gets inherited from
x::threadmsgdispatcherObj
.
This represents the execution thread's message queue.
Performs any additional initialization steps.
Destroys the mcguffin by setting it to a
nullptr
.
Runs the main message dispatching loop by repeatedly invoking
msgqueue_auto
's
event
() method.
The default behavior of
event
() is to wait for a message on the
internal message queue, retrieve it, then invoke the private
class method the message is for.
x::start_threadmsgdispatcher
() does not return
until the new execution thread destroys the mcguffin.
This is why the mcguffin parameter
gets passed to run
() by reference, rather
than value. This ensures that the new threads message queue exists
after
x::start_threadmsgdispatcher
() returns.