Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety and bit-field

I know that bit-field are compiler dependant, but I haven't find documentation about thread safety on bit-field with the latest g++ and Visual C++ 2010.

Does the operations on a bit-field member are atomic ?

like image 407
Errata Avatar asked May 31 '11 19:05

Errata


4 Answers

"Thread safe" is unfortunately a very overloaded term in programming.

If you mean atomic access to bit-fields, the answer is no (at least on all processors I'm aware of). You have atomic access to 32bit memory locations on 32bit machines, but that only means you'll read or write a whole 32 bit value. This does not mean another thread won't do the same thing. If you're looking to stop that you likely want synchronization.

If you mean synchronized access to bit-fields, the answer is also no, unless you wrap your access in a higher level synchronization primitive (which are often built on atomic operations).

In short, the compiler does not provide atomic or synchronized access to bit fields without extra work on your part.

Does that help?

Edit: Dr. Dan Grossman has two nice lectures on atomicity and synchronization I found on UOregon's CS department page.

like image 161
user7116 Avatar answered Sep 21 '22 12:09

user7116


When writing to a bit-field, there may be a time window in which any attempt by another thread to access (read or write) any (same or different) bit-field in the same structure will result in Undefined Behavior, meaning anything can happen. When reading a bit field, there may be a time window when any attempt by another thread to write any bit-field in the same structure will result in Undefined Behavior.

If you cannot practically use separate variables for the bit-fields in question, you may be able to store multiple bit-fields in an integer and update them atomically by creating a union between the bit-field structure and a 32-bit integer, and then using a CompareExchange sequence:

  1. Read the value of the bitfield as an Int32.
  2. Convert it to a bitfield structure
  3. Update the structure
  4. Convert the structure back to an Int32.
  5. Use CompareExchange to overwrite the variable with the new value only if it still holds the value read in (1); if the value has changed, start over with step (1).

For this approach to work well, steps 2-4 must be fast. The longer they take, the greater the likelihood that the CompareExchange in step 5 will fail, and thus the more times steps 2-4 will have to be re-executed before the CompareExchange succeeds.

like image 25
supercat Avatar answered Sep 19 '22 12:09

supercat


If you want to update bitfields in thread-safe way you need to split your bit-fields into separate flags and use regular ints to store them. Accessing separate machine-words is thread-safe (although you need to consider optimizations and cache coherency on multiprocessor systems).

like image 23
John Avatar answered Sep 18 '22 12:09

John


See the Windows Interlocked Functions

Also see this related SO question

like image 40
Doug Currie Avatar answered Sep 22 '22 12:09

Doug Currie