Is there an atomic |= operation?
Asked Answered
T

4

7

Is there such a thing as an atomic |= or and atomic or? If no what is the recommended technique for setting a bit in an variable that needs to be threadsafe? (I am avoiding locks)

Toile answered 25/6, 2011 at 17:1 Comment(0)
Y
7

There is not such thing in C++03, but you can use your vendor specific features. For example you can use InterlockedOr on windows. In C++0x you can use atomic_fetch_or.

Note that atomic operations also require locking, although it's on the hardware level it's still expensive.

Yellowgreen answered 25/6, 2011 at 17:6 Comment(5)
Note that not all atomic operations require locking. There are a couple of non-blocking atomic algorithms out there.Spotweld
@M: atomic operations by their definition require locking. The CPU core doing the operation locks the memory bus for the duration of the operation, so no other CPU can access the memory. Also there is no such thing 'atomic algorithms' so you probably have no idea what are you talking about. sorry.Yellowgreen
@M: when I say 'locking' above I mean hardware-level locking, it's not mutexes.Yellowgreen
@ybungalobill: While technically true, most architectures do technically lock the main bus, lockfree has a different meaning than "doesn't do anything locking." Lockfree, in its proper usage, indicates that given a finite period of time, at least one thread makes progress. Mutexes do not give this guarantee: if a thread acquires a lock, a psychopathic scheduler may choose to only run blocked threads, resulting in no progress. In the case of "locking the memory bus," the processor provides no way to suspend the locking thread without releasing the bus, so it qualifies as "lockfree"Brass
@CortAmmon: All you say is true, but I don't see how it is relevant. I was talking about hardware-level locking (windows API for doing that is named "interlocked-op" not by accident), not lock-free algorithms (which I guess what M Platvoet meant).Yellowgreen
B
4

You can use an atomic compare and swap (CAS), available everywhere you're likely to need it, to make an atomic version of pretty much any operation.

Butterfield answered 25/6, 2011 at 17:49 Comment(5)
Hmm, this would actually work for any operator i need to implement. Good ideaToile
I wonder why "CAS" is called "compare-and-SWAP" rather than "compare-and-STORE", given that the former name makes it sound like "compare-EXCHANGE"--an operation which CAS cannot always emulate without livelock--while the latter name describes what it does?Imitative
@Imitative Because, at least traditionally, it returns the value originally read? Compare and set is closer to what I think you mean, with the same acronym in order to maximise confusion.Butterfield
@AlanStokes: Many descriptions of CAS describe it as having a pass-fail return value (unlike compare-exchange, which returns the observed value in case of failure). On hardware where CAS is pass-fail, an attempt to synthesize compare-exchange could livelock if another processor was alternately setting a variable to the expected value and something else. The compare-exchange code would read the variable, see that it matches expectations, try a conditional store, observe that failed, reread the variable, see the old value, retry the conditional store, etc.Imitative
Realistically, such livelock would be unlikely to persist for very long, but if a CAS fails and the calling code is only interested in a pass-fail indication, there's no reason for a CAS routine to spend extra effort trying to find what the variable might have held when the CAS failed.Imitative
A
3

Take a look at the _InterlockedOr intrinsic. It's the fastest you can possibly get.

Analysand answered 25/6, 2011 at 17:8 Comment(0)
S
1

In the current C++ standard there is no such think - but there will be in C++11 which will be released probably in autumn. See: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1401.pdf

I am not sure, if there are already compilers supporting parts of the new threading facilities of the upcoming C++ standard. Otherwise you would need to make a work around (for example with Boost).

Stich answered 25/6, 2011 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.