locking
How to implement a lock?
Lock is a questionable idea in JS which is intended to be threadless and not needing concurrency protection. You’re looking to combine calls on deferred execution. The pattern I follow for this is the use of callbacks. Something like this: var functionLock = false; var functionCallbacks = []; var lockingFunction = function (callback) { if … Read more
Relative performance of swap vs compare-and-swap locks on x86
I assume atomic_swap(lockaddr, 1) gets translated to a xchg reg,mem instruction and atomic_compare_and_swap(lockaddr, 0, val) gets translated to a cmpxchg[8b|16b]. Some linux kernel developers think cmpxchg ist faster, because the lock prefix isn’t implied as with xchg. So if you are on a uniprocessor, multithread or can otherwise make sure the lock isn’t needed, you … Read more
When does “select for update” lock and unlock?
Locks are taken during (usually at or near the beginning of) a command’s execution. Locks (except advisory locks) are released only when a transaction commits or rolls back. There is no FOR UNLOCK, nor is there an UNLOCK command to reverse the effects of the table-level LOCK command. This is all explained in the concurrency … Read more
What’s the difference between first locking and creating a lock_guard(adopt_lock) and creating a unique_lock(defer_lock) and locking?
1) First code sample { static std::mutex io_mutex; std::lock_guard<std::mutex> lk(io_mutex); std::cout << e1.id << ” and ” << e2.id << ” are waiting for locks” << std::endl; } This is a standard lock guard, when the scope is exited, the lock lk is released { std::unique_lock<std::mutex> lk1(e1.m, std::defer_lock); std::unique_lock<std::mutex> lk2(e2.m, std::defer_lock); std::lock(lk1, lk2); std::cout << … Read more
Does notify/notifyall release the lock being held
No — notify/notifyAll don’t release locks like wait does. The awakened thread can’t run until the code which called notify releases its lock. This is what the Javadoc says: The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object’s monitor to wake up either through a call … Read more
Resource locking with async/await
My question: does anybody know of a good pattern to deal with acquiring exclusive access in the presence of thread switching due to async/await? Yes, you can use AsyncLock, which is also available as part of my AsyncEx library. If you want to have a “TryLock” kind of operation, then you may have to create … Read more
Does the C# Yield free a lock?
No the yield return will not cause any locks to be freed / unlocked. The lock statement will expand out to a try / finally block and the iterator will not treat this any differently than an explicit try / finally in the iterator method. The details are a bit more complicated but the basic … Read more
yield returns within lock statement
UPDATE 2022-Oct Reading this answer after all these years, I felt the original tone was too harsh, and I came off as an a-hole. So I soften the tone a bit. Sorry to resurrect this from the dead, but reading the accepted answer by Daniel, and then testing it myself I though that at least … Read more
What is the equivalent for LOCK_ESCALATION = TABLE in SQL Server 2005?
LOCK_ESCALATION = TABLE is the default behavior in SQL Server 2008 & is the ONLY behaviour in SQL Server 2005. You can safely drop the statement without any change in functionality.