From: Roy S. <roy...@ic...> - 2018-05-02 22:14:41
|
On Wed, 2 May 2018, Tuzun, Robert wrote: > Suppose a user of the class wanted to set or retrieve element n. > This could be done by setting or retrieving bit n%8 of Bytes[n/8]. > > From the previous email's remarks about race conditions, this would > be thread-safe, correct, even when writing to? Nope. This is basically how vector<bool> already works under the hood. The race condition happens when trying to set or retrieve that bit: you look at the current byte, you OR that bit to 1, and you write the result, right? Nope. Because if *another* thread tries to do the same thing, and both look at the current byte at the same time, then neither thread gets the set-to-1 version from the other thread, and only one or the other write actually occurs. There's probably some std::atomic way to fix this but nothing I'd feel safe trying to futz with, and even a perfect fix would necessarily ruin caching and thus speed. --- Roy |