You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The above line translates to the following: data[:mutex] ||= Mutex.new # line 1 mutex = data[:mutex] # line 2
Imagine line 1 gets hit by 2 threads simultaneously: 2 mutexes will be created, and data[:mutex] will be set by first to one, then overwritten by the other.
The following line
mutex ||= (data[:mutex] ||= Mutex.new)can lead to a race condition.The above line translates to the following:
data[:mutex] ||= Mutex.new# line 1mutex = data[:mutex]# line 2Imagine line 1 gets hit by 2 threads simultaneously: 2 mutexes will be created, and data[:mutex] will be set by first to one, then overwritten by the other.