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
0
votes
1 answer

Unexpected output with Lock.acquire() and Lock.release()

from threading import Thread import threading import time def procesa1(): lock = threading.Lock() lock.acquire() for i in range(3): print(threading.get_ident()) lock.release() if __name__ == "__main__": hilos = [] …
user19551894
0
votes
1 answer

Reserving datasets from top to bottom of a list (database-table) for users avoiding race conditions (Django 4.1)

I start developing a Django web app that ist going to be used by hundreds of user at the same time. The app consits of a simple database table that lists unique tasks top-down. The first user that request some tasks should get the tasks from the top…
Thorben
  • 17
  • 6
0
votes
1 answer

Why does removing this barrier create a race condition?

I am trying to understand why the barrier is required to remove the race condtion? #include #include int main() { int sum = 0; #pragma omp parallel num_threads(4) for(int i = 0;i < 10;i++) { #pragma omp…
Niteya Shah
  • 1,809
  • 1
  • 17
  • 30
0
votes
1 answer

Python FastAPI race condition even when requests don't share object

I have a fastAPI app that transforms request payload first for easier processing. (e.g to deal with informal language, reject unknown fields, which fastAPI doesn't provide out-of-box) I do know that this causes race condition (pseudo code): obj =…
Xuekai Du
  • 617
  • 1
  • 6
  • 27
0
votes
0 answers

How to handle race condition in EWS TraceListener while writing traces to the file

I'm trying to write the request response traces to a file in an EWS Managed API client application. The problem is, at a time there are multiple requests/response (multiple emails) traces logged in a file, due to which race condition occurs…
Vishal Dhasal
  • 51
  • 1
  • 10
0
votes
3 answers

Use while loop to make a thread wait till the lock variable is set to avoid race condition in C prgramming

#include #include long mails = 0; int lock = 0; void *routine() { printf("Thread Start\n"); for (long i = 0; i < 100000; i++) { while (lock) { } lock = 1; mails++; lock = 0; } printf("Thread…
Gopendra
  • 15
  • 1
  • 7
0
votes
0 answers

c-ares multi channel/ thread reverse lookup

I am facing race conditions when using c-ares to perform reverse DNS lookups in multiple threads. Code can be found below. static void callback(void * arg, int status, int, struct hostent * host) { auto * ptr_records =…
0
votes
1 answer

How to properly handle race condition caused by retry worker

In one of the services we had some connection issues and we are getting random timeouts (we think it is because of the client library. it is one of the caching services). We decided to handle it by putting it in the queue and retrying on a separate…
0
votes
1 answer

Does any of C++ smart pointers avoid data race in strict sense?

For consumer/producer model there is a built-in mechanism to avoid data race - queue. But for global flag there seems not yet a ready-to-go type to avoid data race rather than attaching a mutex to each global flag as simple as boolean or int type. I…
George Y
  • 525
  • 3
  • 14
0
votes
0 answers

Unexpected behavior when using dictionary with multithreading in swift

I'm attempting to perform numerous asynchronous activities using dispatch queues. My biggest challenge was to have the threads access a dictionary while keeping it safe with an NSLOCK. At the start of my code, I initialized the dictionary so that it…
0
votes
2 answers

Async access to same object

I've been using JS/TS for a while but the existence or not of race conditions is stil confusing me. I am trying to implement the following logic: class Deployer { protected txDataList: Array; constructor() { this.txDataList = new…
weaver
  • 31
  • 1
  • 4
0
votes
2 answers

How can I prevent a race condition in Android RecyclerView's Adapter?

I have a classic implementation of a recycler view that, when I click on an item inside the recycler view, that item gets deleted. The problem is that, when I successively click twice one after another (without any noticeable delay between the…
caveman
  • 422
  • 3
  • 17
0
votes
1 answer

Can't find the race condition

I'm writing a program to count prime numbers from 2 to N using OpenMP. I wrote this code and while testing it, for example, with N = 10 (OMP_NUM_THREADS=2 ./main 10) I get mostly 4-s but sometimes I get 5-s in my output. It seems like there is a…
hiotareq
  • 1
  • 2
0
votes
1 answer

Is it a race condition?

Two threads are executing a function named job, inside which they are incrementing a global variable. Will there be a race condition here? int i = 0; void *job(void *args) { i += 1; }
wowonline
  • 1,061
  • 1
  • 10
  • 17
0
votes
1 answer

Is this some sort of race condition I created with Swift

I have a simple SwiftUI example that moves a block down the screen X pixels each time a timer fires; I wrote the code out in full. issued here can be considered to be zero; vertiSide + horizSide are just reference numbers. Now this works, but I want…
user3069232
  • 8,587
  • 7
  • 46
  • 87