Synchronizing 2 processes using interprocess synchronizations objects – Mutex or AutoResetEvent

I was just going to edit this answer, but it doesn’t seem correct. So I’ll post my own… According to the Threads for C# page, which has a lot of synchronization tutorials, AutoResetEvent cannot be used for interprocess synchronization. However, a named EventWaitHandle can be used for interprocess synchronization. In the above page, visit the … Read more

How to avoid race condition when using a lock-file to avoid two instances of a script running simultaneously?

Yes, there is indeed a race condition in the sample script. You can use bash’s noclobber option in order to get a failure in case of a race, when a different script sneaks in between the -f test and the touch. The following is a sample code-snippet (inspired by this article) that illustrates the mechanism: … Read more

How to implement a unmanaged thread-safe collection when I get this error: is not supported when compiling with /clr

It is not supported because the std::mutex implementation uses GetCurrentThreadId(). That’s a winapi function that is not supposed to be use in managed code since it might be running on a custom CLR host that doesn’t use threads to implement threading. This is the good kind of problem to have, it shows that you are … Read more

C++11 memory_order_acquire and memory_order_release semantics?

The spinlock mutex implementation looks okay to me. I think they got the definitions of acquire and release completely wrong. Here is the clearest explanation of acquire/release consistency models that I am aware of: Gharachorloo; Lenoski; Laudon; Gibbons; Gupta; Hennessy: Memory consistency and event ordering in scalable shared-memory multiprocessors, Int’l Symp Comp Arch, ISCA(17):15-26, 1990, … Read more

Which is more efficient, basic mutex lock or atomic integer?

Atomic operations leverage processor support (compare and swap instructions) and don’t use locks at all, whereas locks are more OS-dependent and perform differently on, for example, Win and Linux. Locks actually suspend thread execution, freeing up cpu resources for other tasks, but incurring in obvious context-switching overhead when stopping/restarting the thread. On the contrary, threads … Read more

Does std::mutex create a fence?

As I understand this is covered in: 1.10 Multi-threaded executions and data races Para 5: The library defines a number of atomic operations (Clause 29) and operations on mutexes (Clause 30) that are specially identified as synchronization operations. These operations play a special role in making assignments in one thread visible to another. A synchronization … Read more