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
2
votes
4 answers

Segfault occuring in attempt to synchronize queue

I am learning about multithreading and I wanted to simulate producer-consumer problem ( using semaphore if I can call it that ). I have a class that holds a queue, producer push ints into queue and consumer retrieves it and prints it. I simulated is…
Darlyn
  • 4,715
  • 12
  • 40
  • 90
2
votes
2 answers

Can this parallel loop cause a data race?

I have a std::vector filled before the parallel loop with std::pair. The bools are all initialised to true. The loop is approximately as follows: for (int x = 0; x < xMax; ++x) // can parallelising the loop in x cause a data race? …
Goobley
  • 331
  • 2
  • 8
1
vote
1 answer

Understanding the working of volatile keyword

class OrderingTest { var x = 0 var y = 0 fun test() { thread { x = 1 y = 1 } thread { val a = y val b = x println("$a, $b") } } } I had this piece of…
cheems
  • 164
  • 7
1
vote
0 answers

Cache line sharing between processors. False sharing. Data race condition?

Trying to simulate data race for code: struct Foo { // ~ 3100ms alignas(std::hardware_destructive_interference_size) std::atomic Counter1 = 0; alignas(std::hardware_destructive_interference_size) std::atomic Counter2 = 0; // ~…
Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15
1
vote
2 answers

Java multithread race condition, Min and Max range of a value

I have been asked this question recently in an interview, and I didn't get a feedback from my interviewer. I think the minimum answer is 2. Given the following code, what will be the minimum and maximum value that will be printed in the line marked…
JohhnyM
  • 31
  • 5
1
vote
1 answer

Does a race happen when using pointers?

For example if I have this code: #include #include #include int *ptr; int main(void) { ptr = malloc(sizeof(int)); if (ptr == NULL) return (1); if (fork() == 0) { (*ptr)++; printf("in child…
user21774205
1
vote
1 answer

What is the difference between "data race" and "atomicity violation" in concurrent programming?

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…
persephone
  • 11
  • 4
1
vote
0 answers

Fixing Data Race

I have a WebSocket client that currently has a data race when connecting to the WS server. The WS client works by connecting to the server, listening to the server connection, and then subscribing to specific WS endpoints. If it matters, I'm using a…
Grant
  • 431
  • 3
  • 8
1
vote
0 answers

Could reading & writing int which is declared with volatile and stored on a aligned address be garanteed to be atomic?

Here the related code snippet: #include #include #include #include #include #ifdef NO_STUCK_WITH_OPTIMIZATION using TYPE = std::atomic; #else using TYPE = volatile int; //The progrom seems…
John
  • 2,963
  • 11
  • 33
1
vote
2 answers

Data race about map::operator[]

Is there any potential problem in this code snippet? #include #include #include #include constexpr int FOO_NUM = 5; int main() { std::map mp; std::vector vec; for(int i=0; i<2; i++) { …
John
  • 2,963
  • 11
  • 33
1
vote
0 answers

SpriteKit - how do I keep game stats thread-safe?

In a SpriteKit game I am developing I get multiple warnings from the Thread Sanitizer. There are some properties of some sprites as well as of the GameScene that are updated asynchronuosly by the player's interaction, for example the player's…
MassMover
  • 529
  • 2
  • 17
1
vote
1 answer

is data race safe in ARMV8?

As we know, access aligned fundamental data types in INTEL X86 architecture is atomic. How about ARMV8? I have tried to get the result from Arm Architecture Reference Manual Armv8, for A-profile architecture, I did find something related to…
Hankin
  • 45
  • 4
1
vote
0 answers

data races in c++ - can they be benign?

I have a multi-threaded c++ program that performs some intensive on-going calculations. I want the program to write the progress to the GUI/console as the calculation progresses. To do so I have the threads set some data regarding their individual…
user3353819
  • 911
  • 2
  • 8
  • 21
1
vote
2 answers

Is it a data race if I enforce 'happens before' relation between conflicting expressions at runtime?

As per cppreference, When an evaluation of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to conflict. A program that has two conflicting evaluations has a data…
Vishal Sharma
  • 1,670
  • 20
  • 55
1
vote
0 answers

Array Function of MPMediaItem Very Slow

I'm trying to edit the queue of my music player using the applicationQueuePlayer and the perform method (details here). However, whenever I apply any array function (map, filter etc.), it takes many seconds to complete, leading to (I think) data…