Questions tagged [race-condition]

A race condition is when multiple threads/processes/clients all use a resource, without proper use of locks.

A race condition is when multiple threads/processes/clients all use a resource, without the proper use of locks.

In multi-threaded programs, race conditions occur because context switches between threads are often erratic and unpredictable, often causing a shared value to change at inopportune times.

Servers are also vulnerable to race conditions, sometimes more so, because there are many independent clients which act independently, as well as delays caused by network communication.

Consider the example of two people simultaneously editing the same document:

  1. Person #1 sees an incorrect line. #1 begins to fix this line, but is a slow typist.
  2. Person #2 sees the same mistake - while #1 is typing - and fixes it.
  3. #1 and #2 commit their edited documents at the same time, without the other being notified.
  4. #1's change reaches the server first, and the server writes it to disk.
  5. #2's change reaches the server later, and the server silently overwrites #1's change.

Locks are a common way to avoid race conditions. Consider the above example with proper locking:

  1. Person #1 sees the incorrect line and starts editing it.
  2. Person #2 also sees the incorrect line, but is unable to edit until Person #1 has pushed their changes to the server.
  3. Person #1's changes are seen by both #1 and #2.
  4. Person #2 decides that #1's changes are adequate, and decides not to edit.

In this scenario, edit access to the document is restricted to one client only. This prevents Person #2 from silently overwriting Person #1's changes. See Wikipedia for further examples.

2338 questions
18
votes
11 answers

Handle same function running and processing the same data at the same time

I have a php system that that allow customer to buy things (make an order) from our system using e-wallet (store credit). here's the database example **sales_order** +--------+-------+----------+--------+--------------+-----------+ |order_id| price…
Hunter
  • 459
  • 4
  • 15
18
votes
6 answers

Race condition and using Google Analytics Asynchronous (_gaq) synchronously

I have a website which is using Google Analytics newer asynchronous tracking method (_gaq). The problem I've run into is that I want to institute some specific link tracking and am worried that I will be creating a race condition. Basically, it's a…
Owen Allen
  • 411
  • 4
  • 11
18
votes
3 answers

How can I ensure a reactjs state is updated, and then call a function?

I'm trying to use reactjs to update a state, and once it is updated fire an ajax call requesting a new page. Just before the ajax call fires an offset variable is set: var offset = this.state.npp * this.state.page; However I find after…
JZ.
  • 21,147
  • 32
  • 115
  • 192
18
votes
1 answer

Can you race condition in Python while there is a GIL?

My understanding is that due to the Global Interpreter Lock (GIL) in cPython, only one thread can ever be executed at any one time. Does this or does this not automatically protected against race conditions, such as the lost update problem?
user2406944
17
votes
4 answers

Does writing the same value to the same memory location cause a data race?

Consider the following code that writes the same value to the same memory location from multiple threads: void f(int* buf, int n, int* p) { for(int i = 0; i < n; i++) buf[i] = i; *p = buf[n/2]; } void g(int* buf, int n) { int…
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
17
votes
5 answers

Race-condition creating folder in Python

I have a urllib2 caching module, which sporadically crashes because of the following code: if not os.path.exists(self.cache_location): os.mkdir(self.cache_location) The problem is, by the time the second line is being executed, the folder may…
dbr
  • 165,801
  • 69
  • 278
  • 343
17
votes
1 answer

Best way to prevent race condition in multiple chrome.storage API calls?

Something requests a task Something else pulls the task list out of storage, and checks if there are tasks there. If there are tasks it removes one and the smaller "task list" is put back in storage. Between steps 2 and 3 a race condition can…
17
votes
1 answer

Are all methods in an iOS app usually in a single thread? (for race condition prevention)

We can have many handlers: touches handler, UIControl handler (buttons, sliders), performSelector, CADisplayLink, NSTimer events, Gesture Recognizer, accelerometer handler, and UIView animation completion block, and some other ones. Are all of them…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
16
votes
2 answers

Does something like CHESS exist for Java?

CHESS is a tool for finding and reproducing Heisenbugs in concurrent programs. CHESS repeatedly runs a concurrent test ensuring that every run takes a different interleaving. If an interleaving results in an error, CHESS can reproduce the…
Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135
16
votes
4 answers

c++ should condition variable be notified under lock

I found the following example for condition variable on www.cppreference.com, http://en.cppreference.com/w/cpp/thread/condition_variable. The call to cv.notify_one() is placed outside the lock. My question is if the call should be made while holding…
Nemo
  • 3,285
  • 1
  • 28
  • 22
16
votes
5 answers

Ajax concurrency

I have a web application where there is a timer that is constantly counting down. Meanwhile, the client frequently checks with the server to see if more time has been added to the timer. The code looks something like this: function tick() { //…
So8res
  • 9,856
  • 9
  • 56
  • 86
15
votes
3 answers

What about race condition in multithreaded reading?

According to an article on IBM.com, "a race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can…
jmich
  • 153
  • 1
  • 5
15
votes
3 answers

Can method inlining optimization cause race conditions?

As seen in this question: Raising C# events with an extension method - is it bad? I'm thinking of using this extension method to safely raise an event: public static void SafeRaise(this EventHandler handler, object sender, EventArgs e) { if…
Jeff Cyr
  • 4,774
  • 1
  • 28
  • 42
15
votes
11 answers

Does one assembler instruction always execute atomically?

Today I came across this question: you have a code static int counter = 0; void worker() { for (int i = 1; i <= 10; i++) counter++; } If worker would be called from two different threads, what value will counter have after both of them…
vava
  • 24,851
  • 11
  • 64
  • 79
14
votes
6 answers

Semaphores and locks in MATLAB

I am working on a MATLAB project where I would like to have two instances of MATLAB running in parallel and sharing data. I will call these instances MAT_1 and MAT_2. More specifically, the architecture of the system is: MAT_1 processes images…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564