What is a race condition?

后端 未结 18 2980
谎友^
谎友^ 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:15

    A race condition is a kind of bug, that happens only with certain temporal conditions.

    Example: Imagine you have two threads, A and B.

    In Thread A:

    if( object.a != 0 )
        object.avg = total / object.a
    

    In Thread B:

    object.a = 0
    

    If thread A is preempted just after having check that object.a is not null, B will do a = 0, and when thread A will gain the processor, it will do a "divide by zero".

    This bug only happen when thread A is preempted just after the if statement, it's very rare, but it can happen.

提交回复
热议问题