1

I am taking an Operating Systems class and we are working in C. My professor says an Atomicity Violation is when the code assumes two accesses are atomic but in reality they are not. Data Race seems to be when a race condition happens. These two seem the same to me, but are apparently different. I don't understand the difference, so I'm hoping someone can give me a little more detail. Maybe some examples as well.

persephone
  • 11
  • 4
  • 1
    What language are you talking about? In C and C++, a "data race" often means data race undefined behaviour, unsynchronized access to non-atomic variables. (Some people argue that race bugs involving atomic variables in lock-free algorithms aren't "data races", and should be called something else.) But "atomicity violation" isn't a term that I've ever heard in C. – Peter Cordes Oct 22 '22 at 20:34
  • I am currently using C in my operating system class. Though my professor did not mention language-specific definitions, I would assume he is working from C. Thank you. – persephone Oct 22 '22 at 20:45

1 Answers1

0

A Data Race occurs when two or more tasks may access data at the same time and one of them is write operation.

Atomicity normally considered the property of an operation taking place in full without any intermediate state being visible.

In C atomicity is often only applied to individual variables but it can extend to more complex structures. But it's actually the operations that are atomic more than the variables.

If one thread is trying to increment an integer reads its value and then writes back that value plus 1 while another thread is doing the same there's a chance that one of the increments may be lost:

Thread A: Reads 7 Thread B: Reads 7 Thread A: Writes 8 Thread B: Writes 8

A write has been lost from the intended outcome.

A atomicity violation occurs when due to incorrect assumptions and fails to ensure atomicity. It's not a term used in C and I'd say it's subsumed by the notion of a data race.

That example of the lost write is a Data Race and an Atomicity Violation. It's common for programmers inexperienced with multi-threading to not realise ++x is not guaranteed to be an atomic operation.

So I'm saying that because a Data Race is reading data while another thread (including process) is writing to it then all Data Races can be characterised as atomicity violations and all atomicity violations can be characterised as data races.

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • In C, data races on non-atomic object are fully undefined behaviour. Symptoms can include optimizing `while(!data_ready){}` into `if(!data_read){ while(1){} }`. That's not tearing or lack of RMW atomicity. [MCU programming - C++ O2 optimization breaks while loop](https://electronics.stackexchange.com/q/387181) – Peter Cordes Oct 23 '22 at 13:42
  • Can you offer an example of something that would be an atomicity violation but not a race condition? – persephone Oct 24 '22 at 17:37
  • @persephone You were talking about **Data Races** before and now **Race Condition**. I'm not trying to be petty but they're slightly different things and how they differ varies depending on your formal system. But no. In general I'd say atomicity violations are race conditions but not necessarily the other way. – Persixty Oct 24 '22 at 17:57
  • @persephone Maybe you're looking for something like a Sequence Lock (https://en.wikipedia.org/wiki/Seqlock). That could be regarded as a way of handling Data Races that doesn't obey atomicity. – Persixty Oct 24 '22 at 18:22