What is a race condition?

后端 未结 18 2978
谎友^
谎友^ 2020-11-21 04:52

When writing multithreaded applications, one of the most common problems experienced is race conditions.

My questions to the community are:

What is the rac

18条回答
  •  萌比男神i
    2020-11-21 05:03

    What is a race condition?

    The situation when the process is critically dependent on the sequence or timing of other events.

    For example, Processor A and processor B both needs identical resource for their execution.

    How do you detect them?

    There are tools to detect race condition automatically:

    • Lockset-Based Race Checker
    • Happens-Before Race Detection
    • Hybrid Race Detection

    How do you handle them?

    Race condition can be handled by Mutex or Semaphores. They act as a lock allows a process to acquire a resource based on certain requirements to prevent race condition.

    How do you prevent them from occurring?

    There are various ways to prevent race condition, such as Critical Section Avoidance.

    1. No two processes simultaneously inside their critical regions. (Mutual Exclusion)
    2. No assumptions are made about speeds or the number of CPUs.
    3. No process running outside its critical region which blocks other processes.
    4. No process has to wait forever to enter its critical region. (A waits for B resources, B waits for C resources, C waits for A resources)

提交回复
热议问题