Questions tagged [data-race]

A data race occurs when:

  • two or more threads in a single process access the same memory location concurrently, and

  • at least one of the accesses is for writing, and

  • the threads are not using any exclusive locks to control their accesses to that memory.

When these three conditions hold, the order of accesses is non-deterministic, and the computation may give different results from run to run depending on that order. Some data-races may be benign (for example, when the memory access is used for a busy-wait), but many data-races are bugs in the program.

(source: https://docs.oracle.com/cd/E19205-01/820-0619/geojs/index.html)

108 questions
137
votes
5 answers

Are "data races" and "race condition" actually the same thing in context of concurrent programming

I often find these terms being used in context of concurrent programming . Are they the same thing or different ?
Inquisitive
  • 7,476
  • 14
  • 50
  • 61
71
votes
3 answers

Multithreading program stuck in optimized mode but runs normally in -O0

I wrote a simple multithreading programs as follows: static bool finished = false; int func() { size_t i = 0; while (!finished) ++i; return i; } int main() { auto result=std::async(std::launch::async, func); …
sz ppeter
  • 1,698
  • 1
  • 9
  • 21
13
votes
5 answers

What formally guarantees that non-atomic variables can't see out-of-thin-air values and create a data race like atomic relaxed theoretically can?

This is a question about the formal guarantees of the C++ standard. The standard points out that the rules for std::memory_order_relaxed atomic variables allow "out of thin air" / "out of the blue" values to appear. But for non-atomic variables, can…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
11
votes
2 answers

IBM example code, non re-entrant functions doesn't work in my system

I was studying re-entrancy in programming. On this site of IBM (really good one). I have founded a code, copied below. It's the first code that comes rolling down the website. The code tries showing the issues involving shared access to variable in…
Daniel Bandeira
  • 360
  • 1
  • 2
  • 12
10
votes
0 answers

Data race reported in Boost lockfree queue by TSan

I'm running the MPMC example given in boost lockfree queue documentation with thread sanitizer and to my surprise this basic example contains data races as per TSan. Any idea what might be wrong? OS: Red Hat Enterprise Linux Server release 7.7 /…
Vishal Sharma
  • 1,670
  • 20
  • 55
9
votes
2 answers

Can multiple threads write the same value to the same variable at the same time safely?

Can multiple threads write the same value to the same variable at the same time safely? For a specific example — is the below code guaranteed by the C++ standard to compile, run without undefined behavior and print "true", on every conforming…
Karu
  • 4,512
  • 4
  • 30
  • 31
8
votes
2 answers

Is this compiler transformation allowed?

Consider this code, where x and y are integers: if (x) y = 42; Is the following compiler transformation allowed ? int tmp = y; y = 42; if (!x) y = tmp; context: This is from Bjarne Stroustrup's FAQ: // start with x==0 and y==0 if (x) y =…
LWimsey
  • 6,189
  • 2
  • 25
  • 53
6
votes
1 answer

Cannot understand go test -race : RACE: DATA WARNING stack trace

I ran into a DATA RACE warning while testing my project, and was wondering if anyone would be kind enough to help me decipher the problem. I have never attempted testing go routines in the past and am finding it hard to wrap my head around data…
nitimalh
  • 919
  • 10
  • 26
5
votes
1 answer

Resolving Data Race in iOS Swift

I have NetworkProvider which will make api call continuously and also once I received the data i will update the userid. At the same time I will access the userid from other functions. This is data race condition, can someone help to remove the…
venky
  • 1,155
  • 1
  • 11
  • 26
4
votes
2 answers

Can changing a value from X to X in C++ lead to a data race?

I have code that works with large data blocks having different layouts. The layout will determine which part of the data is fixed, and which data is not fixed. Once data is fixed in a block, it normally doesn't change anymore. So all code reading…
Patrick
  • 23,217
  • 12
  • 67
  • 130
4
votes
1 answer

Data race in Go: Why does it happen below 10-11ms?

Here is the code I ran: package main import ( "fmt" "time" ) const delay = 9 * time.Millisecond func main() { n := 0 go func() { time.Sleep(delay) n++ }() fmt.Println(n) } Here is the command I used: go…
Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31
4
votes
1 answer

If two unsynchronized threads increment a counter X times, can the total result be less than X?

I have two unsynchronized threads in a tight loop, incrementing a global variable X times (x=100000). The correct final value of the global should be 2*X, but since they are unsynchronized it will be less, empirically it is typically just a bit…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
4
votes
2 answers

Are atomic objects protected against race conditions?

As far as I know they aren't. Atomic objects are free of data races, yet they can still suffer from race conditions: two threads might start in an unpredictable order making the program outcome non-deterministic. Shared data would be "safe"…
Ignorant
  • 2,411
  • 4
  • 31
  • 48
3
votes
2 answers

Reasoning about a program containing a data race

This question follows in continuation of my previous question where my understanding became that 'A data race is a property of an execution, not of the program in the abstract' which means that as long as 2 threads don't access the shared…
Vishal Sharma
  • 1,670
  • 20
  • 55
3
votes
0 answers

When should `std::atomic` be used?

I'm working on a small project of mine that involves the use of multiple threads. The C++ STD library offsers different ways to prevent undefined behavior or data races from accessing the same memory block: std::atomic std::mutex with…
Diego
  • 133
  • 1
  • 12
1
2 3 4 5 6 7 8