title: Prove a thread-locking mechanism prevents data races
body: <p>After having read Matt Gallager's <a href="https://www.cocoawithlove.com/blog/2016/06/02/threads-and-mutexes.html" rel="nofollow noreferrer">Mutexes and closure capture in Swift</a>, I decided I would try to rewrite some of the classes in my Swift project which internally synchronize access to their members. Before trying to do so, however, I noticed something was missing from my current internal synchronization: tests that proved whether data races were actually prevented.</p>
  <p>Intuitively, the locks &quot;should work,&quot; and my program hasn't <em>yet</em> produced any data races (which signals that it at least <em>mostly</em> works) after hundreds of runs; but intuition doesn't always get so far with multi-threading, and not every edge case can be caught under normal conditions.</p>
  <p>Sean Barry's article about writing <a href="https://medium.com/livefront/unit-testing-race-conditions-by-creating-chaos-swift-512a55e09806" rel="nofollow noreferrer">unit tests to simulate race conditions</a>, though helpful for testing the effects of race conditions, is not applicable since I'm concerned about testing data <em>integrity</em> (since race conditions are <a href="https://stackoverflow.com/questions/11276259/are-data-races-and-race-condition-actually-the-same-thing-in-context-of-conc">not the same as data races and don't need to always appear together</a>) - not whether data is accessed in the correct order (of course, race conditions might lead to the wrong data being set at the end, but at least something legible would be there and/or it would make it there without crashing because of a simultaneous write)</p>
  <p>How can I prove that my code prevents data races?</p>
  <p>NOTE: The introduction of Swift <code>actor</code>s will guarantee that my code is data race-free (though not race condition-free) and my classes will likely turn into actors; but since this question could be considered language-agnostic, it would still be good to know how to prove data races are prevented.</p>