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
1
vote
1 answer

How to check if a file is used by another process in C++?

I need to check if a file is currently opened by another process, e.g. a text editor (but needs to apply to everything else too). I tried using std::ofstream::is_open() etc., but this did not work. I could open the file in my text editor while my…
Oscar K
  • 185
  • 1
  • 11
1
vote
3 answers

why reading a variable modified by other threads can be neither old value nor new value

It has been mentioned by several, for example here c++ what happens when in one thread write and in second read the same object? (is it safe?) that if two threads are operating on the same variable without atomics and lock, reading the variable can…
1a1a11a
  • 1,187
  • 2
  • 16
  • 25
1
vote
1 answer

Is using -fopenmp necessary when using thread sanitizers in clang or gcc

I am trying to use threadsanitizer on given piece of code(in ok.c file) as: clang -fsanitize=thread ok.c -w -I../runtime This works fine and no data race is detected, but when I try giving -fopenmp option to sanitizer it dumps the terminal with…
Sameeran Joshi
  • 59
  • 1
  • 10
1
vote
0 answers

Does data race happen when using cgo?

Goroutines runs in different stack for cgo and go: C doesn’t know anything about Go’s calling convention or growable stacks, so a call down to C code must record all the details of the goroutine stack, switch to the C stack, and run C code which…
Yang
  • 759
  • 2
  • 9
  • 30
1
vote
1 answer

What is causing this data race?

Why does this code cause data race? I have already used atomic add. package main import ( "sync/atomic" "time" ) var a int64 func main() { for { if a < 100 { atomic.AddInt64(&a, 1) go run() } …
fatDuo
  • 23
  • 2
1
vote
1 answer

Confusion in Multithreading in C++

I am trying to simulate a probability problem, in which there are n clients and n servers. Each client randomly sends a request to any server, so each server can receive any number of requests, I have to calculate the expected number of maximum…
0
votes
0 answers

Is reading again and again to see a particular write from another thread, a data race condition?

Consider the following piece of multiple - threaded code - #include #include using namespace std; int a = 0; bool alive = 1, start = 0; void detector() { int local_a = 0; while (alive) { if (start) { …
0
votes
1 answer

In C++, does a data race in one possible scenario render the program's behaviour undefined even if that scenario never gets executed?

The following program contains a data race: #include #include #include #include using namespace std::chrono_literals; int sharedVar = 42; void func(const std::string& threadName) { int input; …
Vishal Sharma
  • 1,670
  • 20
  • 55
0
votes
0 answers

C++ Destroy object with a mutex

I am making a command prompt that can create and remove (destroy) objects. You can interact with the objects via another commands and the values are sometimes exported (on request). What if someone tries to delete object that is currently exporting…
nworder1
  • 1
  • 1
0
votes
1 answer

Python threading memory error / bug / race condition

I have an app where the following happens: starts a thread to generate "work" this thread then starts a thread pool with 5 workers to generate "work" and put it on to a FIFO queue starts a thread pool of 20 workers to get work from the FIFO…
0
votes
1 answer

How can I parallelize with OMP, with data dependencies for two matrices

The following code shown is used to calculate the inverse of a matrix by the Gauss Jordan method, halving memory accesses. This improves single thread execution time. The problem I'm having is that new data dependencies are created that prevent me…
0
votes
2 answers

How to run two loops from two threads one by one, like a flip flop?

I have a question which similarly answered here but it is not exactly what I need. I have two threads, each has a loop. Now I want to force two threads to work like a flip flop. exactly like this ABABAB or BABABA... it is not important who start…
0
votes
0 answers

Optimization, global variables and memory-barriers

In the following example the variable v is not accessible from outside this TU. So the compiler can optimize the loop in main() to an empty loop. This is done with -O3, but with -Os it remains a loop with the load of v outside the loop, so that it…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
0
votes
1 answer

Data race in parallelized nested loop

I have a triple nested loop that I would like to parallelize, however, I am getting a data race issue. I am pretty sure that I need to use a reduction somehow, but I don't quite know how. This is the loop in question: #pragma omp parallel for simd…
koipond
  • 306
  • 2
  • 8
0
votes
2 answers

Elegant way to handle data race caused by passing channel and shared memory

Here is one case of Data Race Patterns in Go Mixed use of message passing (channels) and shared memory makes code complex and susceptible to data races func (f *Future) Start() { go func() { resp, err := f.f() f.resp = resp …
zangw
  • 43,869
  • 19
  • 177
  • 214