1

I have a situation where I'm having to use a blackboxed wrapper for multithreading (which I suspect sits on top of a TBB Thread Pool).

I have a value that can only be obtained by an object that has an expensive constructor, and each thread needs a local instance of that, which is OK. That object will produce a value that is guaranteed always identical across threads (all constructors take the same const forming argument from the main loop). Each thread also has access to a shared struct for that argument as well as to save some results.

The value in question (an iteration range in the form of unsigned int) required by the threads is used later in the main loop, so if I could I'd rather not create another expensive instance of the above mentioned object just to get that same value again.

My question is, on Windows with VC11, and Linux with GCC 4.8.2, on x86-64, is writing the same value to the same memory location (int in a struct the threads have a pointer to) from multiple threads a benign race? Is it a race I could just let happen without guarding the value with an expensive lock? From cursory testing it seems to be the case, but I'm not entirely sure whether under the hood the operation is atomic and safe, or if there's a potential for corruption that might show up under stress.

4
  • 3
    Would std::atomic be a solution for you?
    – stefaanv
    Commented Feb 4, 2015 at 8:26
  • I could always preempt a potential problem with a spinlock or using an atomic attribute, sure, but for my benefit and for the future I'd really like to know if racing the setting of a value like I indicated is safe/benign or has fringe or stress cases I'm not aware of. I know it's a platform dependent issue usually, but I only ever work to the platforms indicated. Thanks for your interest.
    – user349594
    Commented Feb 4, 2015 at 8:29
  • You said you're using a blackboxed wrapper. Are you sure the threads don't interact and maybe already have a locking mechanism for the structure?
    – ollo
    Commented Feb 4, 2015 at 9:01
  • I've tested that while troubleshooting another spot, I could actually have them race the data (stuff was set interleaved at random each execution VS ordered or set to last thread only when I manually locked it). There is also no indication in the documentation about any locks or inherent threadsafety, data is passed as a void pointer and is recast locally to the thread manually and left to the user.
    – user349594
    Commented Feb 4, 2015 at 9:25

1 Answer 1

3

If the data race is "benign" or not really depends on the compiler and runtime platform. Compilers assume programs are race-free and and behaviour resulting from race conditions is undefined. Using atomic operations does not incur much overhead and is recommended in this situation.

Some fringe cases and very good examples on what could go wrong can be found here: https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong

In his post ThreadSanitizer developer Dmitry Vyukov writes "So, if a data race involves non-atomic writes, it always can go wrong".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.