Overhead of pthread mutexes?

All modern thread implementations can handle an uncontended mutex lock entirely in user space (with just a couple of machine instructions) – only when there is contention, the library has to call into the kernel. Another point to consider is that if an application doesn’t explicitly link to the pthread library (because it’s a single-threaded … Read more

Do mutexes guarantee ordering of acquisition? Unlocking thread takes it again while others are still waiting

Known problem. C++ mutexes are thin layer on top of OS-provided mutexes, and OS-provided mutexes are often not fair. They do not care for FIFO. The other side of the same coin is that threads are usually not pre-empted until they run out of their time slice. As a result, thread A in this scenario … Read more

If you unlock an already unlocked mutex, is the behavior undefined?

For pthreads it will result in undefined behaviour. From the man page for pthread_mutex_unlock: Calling pthread_mutex_unlock() with a mutex that the calling thread does not hold will result in undefined behavior. Other mutexes will have their own behviour. As others have stated, it’s best to read the manual for whichever mutex you’re using.

Why do the Go-generated protobuf files contain mutex locks?

The impl.MessageState is embedded in concrete messages only, not in the generated structs that implement a proto message. It specifically embeds the three pragmas: NoUnkeyedLiterals, DoNotCompare, and DoNotCopy. The last one, DoNotCopy is a zero-sized array of sync.Mutex. The sole purpose is to have go vet complain loudly about shallow copies, as described in the … Read more

Massive CPU load using std::lock (c++11)

On my machine, the following code prints out 10 times a second and consumes almost 0 cpu because most of the time the thread is either sleeping or blocked on a locked mutex: #include <chrono> #include <thread> #include <mutex> #include <iostream> using namespace std::chrono_literals; std::mutex m1; std::mutex m2; void f1() { while (true) { std::unique_lock<std::mutex> … 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

How Compare and Swap works

“general pseudo code” is not an actual code of CAS (compare and swap) implementation. Special hardware instructions are used to activate special atomic hardware in the CPU. For example, in x86 the LOCK CMPXCHG can be used (http://en.wikipedia.org/wiki/Compare-and-swap). In gcc, for example, there is __sync_val_compare_and_swap() builtin – which implements hardware-specific atomic CAS. There is description … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)