Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in which cases do I need to lock a variable from simultaneous access?

In a C or C++ program, if 2 threads use the same global variable, then you need to lock the var via a mutex.

But in which cases exactly?

  1. Thread 1: read Thread 2: read
  2. Thread 1: write Thread 2: read
  3. Thread 1: write Thread 2: write

Of course you need to lock at case 3 but what is with the other 2 cases? What happens at case 2 (with non atomic operations)? Is there some kind of access violation or does Thread 2 just get the old value? I'm a litle confused about this, because memory and registers on the hardware level can't be accessed at the same time (in normal PC hardware) or do we have some kind of parallel CPUs with parallel bus lines to parallel ram chips?

like image 228
scheiflo Avatar asked Aug 02 '13 12:08

scheiflo


People also ask

When would you use the lock keyword?

The lock keyword is used to get a lock for a single thread. A lock prevents several threads from accessing a resource simultaneously. Typically, you want threads to run concurrently. Using the lock in C#, we can prevent one thread from changing our code while another does so.

Why are locks needed in a multi threaded program?

A lock may be a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: just one thread at a time can acquire the lock and everyone accesses to the shared resource requires that the lock be acquired first.

Why do we need locks?

Locks are used to make a river more easily navigable, or to allow a canal to cross land that is not level. Later canals used more and larger locks to allow a more direct route to be taken.

What is a lock concurrency?

A lock is designed to enforce a mutual exclusion concurrency control policy, and with a variety of possible methods there exists multiple unique implementations for different applications.


1 Answers

Just think of what may happen in each of the cases. Let's only consider the race condition: it's easy and it's enough for us to see the consequences.

In case 1, the variable is not being modified, so no matter which the order is, both threads will read the same value. So basically, nothing is wrong here.

Cases 2 and 3 are worse. Let's say you have a race condition, and don't know which of the threads will get access earlier. That means:

For case 2: The value of the variable in the end of all operations is fine (it will be the value written by Thread 1), but Thread 2 may get an old value of the variable, which may cause a crash, or other problems.

For case 3: The end value of the variable is not predictable, since it depends on which thread will perform the write operation last.

For cases 2 and 3, it may also happen that one of the threads will try to access the variable while it is in an inconsistent state, and you may end up with some rubbish data read by one of the threads (i.e. Case 2), or even rubbish data in the variable after completion of all operations.

So yea, lock for cases 2 and 3.

like image 81
SingerOfTheFall Avatar answered Sep 29 '22 08:09

SingerOfTheFall