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
1304
votes
18 answers

What is a race condition?

When writing multithreaded applications, one of the most common problems experienced is race conditions. My questions to the community are: What is the race condition? How do you detect them? How do you handle them? Finally, how do you prevent them…
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24
141
votes
8 answers

Is the != check thread safe?

I know that compound operations such as i++ are not thread safe as they involve multiple operations. But is checking the reference with itself a thread safe operation? a != a //is this thread-safe I tried to program this and use multiple threads…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
125
votes
7 answers

How to get last inserted row ID from WordPress database?

My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion. The feature is to using AJAX to post data to server to insert into DB. The…
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
108
votes
5 answers

Why does code mutating a shared variable across threads apparently NOT suffer from a race condition?

I'm using Cygwin GCC and run this code: #include #include #include using namespace std; unsigned u = 0; void foo() { u++; } int main() { vector threads; for(int i = 0; i < 1000; i++) { …
mafu
  • 31,798
  • 42
  • 154
  • 247
78
votes
5 answers

Difference between racearound condition and deadlock

What is the difference between a dead lock and a race around condition in programming terms?
ckv
  • 10,539
  • 20
  • 100
  • 144
74
votes
6 answers

Atomic increment of a counter in django

I'm trying to atomically increment a simple counter in Django. My code looks like this: from models import Counter from django.db import transaction @transaction.commit_on_success def increment_counter(name): counter =…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
61
votes
2 answers

Atomic UPDATE .. SELECT in Postgres

I'm building a queuing mechanism of sorts. There are rows of data that need processing, and a status flag. I'm using an update .. returning clause to manage it: UPDATE stuff SET computed = 'working' WHERE id = (SELECT id from STUFF WHERE computed IS…
57
votes
9 answers

Private constructor to avoid race condition

I am reading the book Java Concurrency in Practice session 4.3.5 @ThreadSafe public class SafePoint{ @GuardedBy("this") private int x,y; private SafePoint (int [] a) { this (a[0], a[1]); } public SafePoint(SafePoint p) {…
peter
  • 8,333
  • 17
  • 71
  • 94
52
votes
5 answers

MySQL INSERT IF (custom if statements)

First, here's the concise summary of the question: Is it possible to run an INSERT statement conditionally? Something akin to this: IF(expression) INSERT... Now, I know I can do this with a stored procedure. My question is: can I do this in my…
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
51
votes
1 answer

WRITE_ONCE in linux kernel lists

I am reading the linux kernel implementation of doubled linked list. I do not understand the use of the macro WRITE_ONCE(x, val). It is defined as follow in compiler.h: #define WRITE_ONCE(x, val) x=(val) It is used seven times in the file, such…
Gaut
  • 1,255
  • 2
  • 15
  • 33
49
votes
6 answers

Race conditions in django

Here is a simple example of a django view with a potential race condition: # myapp/views.py from django.contrib.auth.models import User from my_libs import calculate_points def add_points(request): user = request.user user.points +=…
Fragsworth
  • 33,919
  • 27
  • 84
  • 97
45
votes
2 answers

SQL Server Process Queue Race Condition

I have an order queue that is accessed by multiple order processors through a stored procedure. Each processor passes in a unique ID which is used to lock the next 20 orders for its own use. The stored procedure then returns these records to the…
William Edmondson
  • 3,619
  • 3
  • 32
  • 41
42
votes
4 answers

Understanding goroutines

I'm trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program: package main import "fmt" var x = 1 func inc_x() { //test for { x += 1 } } func main() { go inc_x() for { fmt.Println(x) } } I…
user793587
40
votes
4 answers

Do database transactions prevent race conditions?

It's not entirely clear to me what transactions in database systems do. I know they can be used to rollback a list of updates completely (e.g. deduct money on one account and add it to another), but is that all they do? Specifically, can they be…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
40
votes
3 answers

Helgrind (Valgrind) and OpenMP (C): avoiding false positives?

The documentation for the Valgrind thread error detection tool Helgrind, found here warns that, if you use GCC to compile your OpenMP code, GCC's OpenMP runtime library (libgomp.so) will cause a chaos of false positive reports of data races, because…
Amittai Aviram
  • 2,270
  • 3
  • 25
  • 32
1
2 3
99 100