in event-driven application have common issue. suppose following class diagram:
i'm using observer pattern here. worker
, workermanager
lives in same thread. suppose sequence of actions:
worker
calls listener class report result in middle of methodworkermanager
(which listener ofworker
) decides delete/modifyworker
classworker
continue execution of method, being removed of modified. programmer must consider these situations, , complicated , error-prone.
in code might that:
// in worker.cpp void worker::somemethod() { ... listener_->onworkdone(this); // removed here listener workdone_ = true; // memory corruption ... } // in worker_manager.cpp void workermanager::onworkerworkdone(worker* worker) { removeworker(worker); }
how can modified:
// in worker.cpp void worker::dosomework() { workdone = false; ... } void worker::somemethod() { ... listener_->onworkdone(this); // sets workdone false here dosomework() workdone_ = true; // somemethod doesn't aware of ... } // in worker_manager.cpp void workermanager::onworkerworkdone(worker* worker) { worker->dosomework(); // work }
now see these solutions problem:
- set code style guide listener class must called last. quite fragile , error prone solution.
- made sort of qt queued connections.
qt queued connections made on meta-object system , way project. need simpler tool allow me defer listener callbacks in thread's event loop.
i believe issue common in event-driven design. can see examples of how can handled?
Comments
Post a Comment