Is there a race condition problem in my code?

Asked

Viewed 67 times

2

Is there any problem of race condition in the code below?

Private Shared Sub TestRandomNumberGeneration(ByVal random As Random)
    Dim failed As Boolean = False
    Parallel.For(0, 100000, Sub(i, state)
        Dim [next] As Integer = random.Next()
        If [next] = 0 Then
            Volatile.Write(failed, True)
            state.Stop()
        End If
    End Sub)
    Assert.IsFalse(failed)
End Sub
  • Thank you so much for formatting :D

  • You’re welcome! Now in color :)

  • Ficooooou mass :D :D :D :D

  • 1

    @Horacio.C.Son, you can learn the essentials of editing here: http://answall.com/editing-help for the next questions :)

1 answer

2


It depends. Whether the method Stop of the object state can only be called once (which is not clear in the question), so yes, there is a possibility, however small, of more than one thread calling this method. And it is possible, even without the parallelism, that the Assert.IsFalse(failed) fail - zero is a possible result for function random.Next() (assuming that random is the type System.Random).

If the method Stop may be called more than once, and if it is acceptable that the Assert.IsFalse fail, so there is a race condition in the code. It depends on the hypothesis you have about your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.