Have you ever run a race?

If so, you already know that a photo finish is sometimes required to pick the winner!

But what if two people arrived at the same time at the finish line?

synology nas

Welcome to race conditions.

What AreComputer Threads?

To explain a race condition, we first have to understand a little about how computers work internally.

The main page of the Homepage homelab dashboard with services running on it.

Each of those actions will result in a reaction by the operating system to start a new computing/computer thread.

In Linux, such a thread is uniquely identified by aPID(Process Identifier).

The latter two refer to the identification of devices and are unrelated to process management.

Tux mascot jumping from Windows to Linux.

When a thread starts, it can in itself start other threads.

The original thread is often referred to as the main or the parent thread.

You may also think about threads like runners in a race.

Article image

Juice Dash/Shutterstock.com

For example, think about a busy database server serving many different connected clients.

then sooner or later it may run into a race condition.

What Is aRace Condition?

It is possible, though quite unlikely, for this to occur in human races.

For computers that process thousands of operations per millisecond, it becomes a lot more feasible.

There are now two runners grabbing for the baton.

Race conditions can happen in various areas like inside electronics, in computer software, and general life.

A user has just committed a form, and the backend software is writing this form into memory.

Simultaneously, another user is reading out the fields of this form from the same memory space.

Depending on what happens, the reading user may receive a partially incorrect form with partially updated information.

An often-used term isthread safetyor athread safeapplication or programming language [construct].

If software is deemedthread safe, it is deemed to be free of the possibility of race conditions.

Various programming constructs may be used to prevent race conditions.

For example, semaphores and mutexes.

In Bash, however, one does not find such a construct natively.

Implementing even basic thread-safety handling constructs can be a complex matter.

For example, consider the difficulty of implementing aSemaphore in Bash.

Wrapping up

In this article, we explored computing threads and race conditions.