1

I adopt definition of race condition as follows from everything2.com:

A race condition is a situation where running multiple concurrent processes (for these purposes, a thread is also modeled as a process, as are processes running on separate machines) will give differing results, depending on the (unspecified, and usually unspecifiable) details of the ordering of operations.

Now consider following sample from Chapter 3: Race Conditions and Mutual Exclusion:

The most dangerous race conditions, however, involve access to shared data structures. If two threads are updating the same data structure at the same time, the changes may be made partially by one thread and partially by the other. The contents of the data structure can then become garbled, which will confuse threads that access it later, thereby causing them to crash.

Obviously, this sample includes a harmful race condition.

There is a solution to avoid this harmful race condition:

The trick is to force the threads to access the data structure one at a time, so called mutual exclusion, so that each thread can complete its update and leave the structure in a consistent state for the next thread.

Now according to above definition, there is still a race condition in above solution:

If thread A makes change first, then final content of shared data structure is filled by thread B; if thread B makes change first, then final content of shared data structure is filled by thread A.

But now it is a harmless race condition.

From this sample, I get following conclusion:

A race condition is harmful if and only if it is avoidable.

I'm not sure whether both direction or just one direction of above conclusion are correct or incorrect. So is there any sample to disprove this conclusion?

newman
  • 499
  • 3
  • 10
  • 4
    This is very similar in theme to most of your other questions. Out of curiosity, are you writing a paper? – Paul Bellora Aug 30 '12 at 03:56
  • possible duplicate of [Is this a race condition?](http://stackoverflow.com/questions/12007205/is-this-a-race-condition) – assylias Aug 30 '12 at 08:26

3 Answers3

2

You are right.

Race Conditions- Arise when multiple threads access the same resources.
A code section that leads to race conditions is called a critical section. Note that deadlock occurs when we are trying to avoid race conditions.

You can avoid race conditions by creating thread-safe code.

  • Local variables.

    Local variables are stored in each thread's own stack. That means that local variables are never shared between threads.

  • Local Object references.

    All objects are stored in the shared heap but if the object created locally never escapes the method it was created in, then it is thread safe.

  • Making Member fields volatile, final and private.

But if you want to share a variable and still control it's access in a synchronized manner. You can do that in the following ways,

  • Using Explicit Locks (and Condition Variables)
  • Using synchronized (Implicit Locks)

This still doesn't guarantee which thread gets access first and which next. For such a control over threading use signals.

It can be achieved in various ways.

  • Signaling via Shared Objects
  • Busy Wait
  • wait(), notify(), and notifyAll()
  • Missed Signals
  • Spurious Wakeups
John Eipe
  • 10,922
  • 24
  • 72
  • 114
1

You are missing out on A very important point here. Any operation on data structure takes place in few cycles,e.g. to give a very basic steps in addition ,

step 1. retrieval of the current value of variable step 2. Add desired number to the value of variable step 3. Store the value of variable

At register level, you can see more number of steps. But the point is , avoiding race condition by locking the access of variable/data structure means that the only one thread can read / write on that data structure . other thread will not be allowed to read the data structure and once 1st thread completes all the cycles , it will allow other threads to do the changes .

First thing what a thread does is Acquire the Lock if the data structure is not locked by anyone. then it does the reading /writing stuff.

So, there shouldn't be any race condition if you implement locks properly.

Race condition exist when two or more threads could make changes in same data structure, and It could be harmful if it is not avoided through locks or some other mechanism.

Avichal Badaya
  • 3,423
  • 1
  • 21
  • 23
0

I disagree. A change is "harmful" when you do not want it, whether it is avoidable or not.

Obviously when two processes run in parallel, then there is always racing going on to change that data, and thus also the end result of the state might depend on that racing.

But when talking about a race conditions: a harmful race condition happens when the consistency of that data might be broken by racing processes.

Dirk Bonné
  • 648
  • 5
  • 12