Simple Deadlock Examples

Maybe a simple bank situation. class Account { double balance; void withdraw(double amount){ balance -= amount; } void deposit(double amount){ balance += amount; } void transfer(Account from, Account to, double amount){ sync(from); sync(to); from.withdraw(amount); to.deposit(amount); release(to); release(from); } } Obviously, should there be two threads which attempt to run transfer(a, b) and transfer(b, a) at … Read more

Do lock-free algorithms really perform better than their lock-full counterparts?

Beyond the simple cases of the InterlockedXxx functions, it seems like the prevailing pattern with all of these is that they implement their own locks. None of the answers here really seem to get to the heart of the difference between a “lock-free” CAS loop and a mutex or spin-lock. The important difference is that … 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