Questions tagged [atomicity]

In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously. Atomicity is a guarantee of isolation from concurrent processes. Additionally, atomic operations commonly have a succeed-or-fail definition — they either successfully change the state of the system, or have no visible effect.

In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously. Atomicity is a guarantee of isolation from concurrent processes. Additionally, atomic operations commonly have a succeed-or-fail definition — they either successfully change the state of the system, or have no visible effect.

Atomicity is commonly enforced by mutual exclusion, whether at the hardware level building on a cache coherency protocol, or the software level using semaphores or locks. Thus, an atomic operation does not actually occur instantaneously. The benefit comes from the appearance: the system behaves as if each operation occurred instantly, separated by pauses. Because of this, implementation details may be ignored by the user, except insofar as they affect performance. If an operation is not atomic, the user will also need to understand and cope with sporadic extraneous behaviour caused by interactions between concurrent operations, which by its nature is likely to be hard to reproduce and debug.

Source: Wikipedia

574 questions
26
votes
4 answers

Is the "switch" statement evaluation thread-safe?

Consider the following sample code: class MyClass { public long x; public void DoWork() { switch (x) { case 0xFF00000000L: // do whatever... break; case 0xFFL: …
Mario Vernari
  • 5,338
  • 1
  • 26
  • 39
25
votes
2 answers

should LOCK_EX on both read & write be atomic?

file_put_contents ( "file", "data", LOCK_EX ) for writing (which means - aquire lock and write) file_get_contents ( "file", LOCK_EX ) for reading (which means - aquire lock and then read) will it throw exception? raise an error? block until lock…
Kamil Tomšík
  • 2,288
  • 2
  • 26
  • 32
25
votes
3 answers

On a multicore x86, is a LOCK necessary as a prefix to XCHG?

If mem is a shared memory location, do I need: XCHG EAX,mem or: LOCK XCHG EAX,mem to do the exchange atomically? Googling this yields both yes and no answers. Does anyone know this definitively?
Walter Bright
  • 4,131
  • 1
  • 21
  • 27
23
votes
3 answers

std::atomic decrement and comparison

On the following code: std::atomic myint; //Shared variable //(...) if( --myint == 0) { //Code block B } Is it possible that more than one thread access the block I named "Code Block B"? Please consider that overflow will not happen, that…
André Puel
  • 7,716
  • 7
  • 45
  • 78
19
votes
3 answers

Is x86 CMPXCHG atomic, if so why does it need LOCK?

The Intel documentation says This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. My question is Can CMPXCHG operate with memory address? From the document it seems not but can anyone confirm that…
Alex Suo
  • 2,661
  • 1
  • 10
  • 21
18
votes
4 answers

Simulate tearing a double in C#

I'm running on a 32-bit machine and I'm able to confirm that long values can tear using the following code snippet which hits very quickly. static void TestTearingLong() { System.Threading.Thread A = new…
Michael Covelli
  • 1,931
  • 3
  • 25
  • 39
18
votes
4 answers

Difference between getAndSet and compareAndSet in AtomicBoolean

The thread title should be self-explnatory... I'm a bit confused between the specification of below methos from AtomicBoolean class: java.util.concurrent.atomic.AtomicBoolean#compareAndSet java.util.concurrent.atomic.AtomicBoolean#getAndSet My…
tmarwen
  • 11,491
  • 4
  • 35
  • 54
18
votes
2 answers

When is AtomicInteger preferrable over synchronized?

Since AtomicInteger can be at at least an order of magnitude slower than an int protected by synchronized, why would I ever want to use AtomicInteger? For example, if all I want is to increment an int value in a thread-safe manner, why not always…
ef2011
  • 9,581
  • 12
  • 45
  • 67
17
votes
5 answers

C# fundamentally not portable?

I've been using C# for a while, and have recently started working on adding parallelism to a side project of mine. So, according to Microsoft, reads and writes to ints and even floats are atomic I'm sure these atomicity requirements workout just…
Jake
  • 181
  • 1
  • 5
17
votes
5 answers

Can a database support "Atomicity" but not "Consistency" or vice-versa?

I am reading about ACID properties of a database. Atomicity and Consistency seem to be very closely related. I am wondering if there are any scenarios where we need to just support Atomicity but not Consistency or vice-versa. An example would really…
rkg
  • 5,173
  • 7
  • 32
  • 47
16
votes
2 answers

Is pointer assignment atomic in C++?

I've actually heard claims both ways. I suspect they are not, but I wanted to get the topic settled.
fbrereto
  • 34,250
  • 17
  • 118
  • 176
16
votes
1 answer

Release a socket back depending on whether they are live or dead in its own pool?

I am using below class to send data to our messaging queue by using socket either synchronously or asynchronously as shown below. It depends on requirement whether I want to call synchronous or asynchronous method to send data on a socket. Most of…
john
  • 9,493
  • 34
  • 111
  • 210
16
votes
3 answers

How to move an element in a sorted list and keep the CouchDb write "atomic"

I have elements of a list in couchdb documents. Let's say these are 3 elements in 3 documents: { "id" : "783587346", "type" : "aList", "content" : "joey", "sort" : 100.0 } { "id" : "358734ff6", "type" : "aList", "content" : "jill", "sort" : 110.0…
user89021
  • 13,714
  • 14
  • 51
  • 65
14
votes
1 answer

ARM: Is writing/reading from int atomic?

On ARM architecture, unfortunately I don't know exactly what chip it is, is a 32 bit int read/write atomic? Is there any sort of guarantees about reads/writes to basic types?
Tony The Lion
  • 57,181
  • 57
  • 223
  • 390
14
votes
8 answers

How to implement a concurrent circular ticker (counter) in Java?

I want to implement a circular counter in Java. The counter on each request should increment (atomically) and on reaching an upper limit should roll over to 0. What would be the best way to implement this and are there any existing implementations?
sheki
  • 8,093
  • 10
  • 45
  • 68
1
2
3
38 39