How can I reproduce the race conditions in this python code reliably?

Traditionally, forcing race conditions in multithreaded code is done with semaphores, so you can force a thread to wait until another thread has achieved some edge condition before continuing. For example, your object has some code to check that start is not called if the object is already running. You could force this condition to … Read more

How to make sure there is no race condition in MySQL database when incrementing a field?

Here’s 3 different approaches: Atomic update update table set tries=tries+1 where condition=value; and it will be done atomically. Use row locking If you do need to first select the value and update it in your application, you likely need to use row locking. That means you’ll have to use InnoDB, not MyISAM tables. Your query … Read more

Adding to a generic dictionary causes IndexOutOfRangeException

You should have looked to the documentation. That what it says: A Dictionary can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during … Read more

Preventing race condition of if-exists-update-else-insert in Entity Framework

When using serializable transaction SQL Server issues shared locks on read records / tables. Shared locks doesn’t allow other transactions modifying locked data (transactions will block) but it allows other transactions reading data before the transaction which issued locks start modifying data. That is the reason why the example doesn’t work – concurrent reads are … Read more

How to correctly use sync.Cond?

OP answered his own, but did not directly answer the original question, I am going to post how to correctly use sync.Cond. You do not really need sync.Cond if you have one goroutine for each write and read – a single sync.Mutex would suffice to communicate between them. sync.Cond could useful in situations where multiple … Read more

Why does Python threading.Condition() notify() require a lock?

This is not a definitive answer, but it’s supposed to cover the relevant details I’ve managed to gather about this problem. First, Python’s threading implementation is based on Java’s. Java’s Condition.signal() documentation reads: An implementation may (and typically does) require that the current thread hold the lock associated with this Condition when this method is … Read more