Mir

Code review comment for lp:~raof/mir/fix-threaded-dispatcher-death-test-race

Revision history for this message
Chris Halse Rogers (raof) wrote :

This code:

{
    md::ThreadedDispatcher* dispatcher;

    auto dispatchable = std::make_shared<mt::TestDispatchable>([&dispatcher]() { delete dispatcher; });

    dispatcher = new md::ThreadedDispatcher("Death thread", dispatchable);

    dispatchable->trigger();
}

invokes undefined behaviour. The read for “delete dispatcher” (which happens in the thread created in the ThreadedDispatcher) conflicts with the write to dispatcher (after ThreadedDispatcher's constructor has run).

The read for “delete dispatcher” is triggered by dispatchable->trigger(), but dispatchable->trigger() is *not* a synchronisation point - there are no mutexes or memory barriers involved.

std::atomic<> is exactly the tool required to solve this. (I guess we could also use std::atomic_thread_fence, but that's essentially a larger hammer for the same thing).

« Back to merge proposal