In the figure to the right, suppose A and B are making simultaneous transfers between two accounts in our bank.Ī transfer between accounts needs to lock both accounts, so that money can’t disappear from the system.Ī and B each acquire the lock on their respective “from” account: A acquires the lock on account 1, and B acquires the lock on account 2. This avoids the problem of reordering, ensuring that the owner of a lock is always looking at up-to-date data. Using a lock also tells the compiler and processor that you’re using shared memory concurrently, so that registers and caches will be flushed out to shared storage. Release relinquishes ownership of the lock, allowing another thread to take ownership of it. If a thread tries to acquire a lock currently owned by another thread, it blocks until the other thread releases the lock.Īt that point, it will contend with any other threads that are trying to acquire the lock.Īt most one thread can own the lock at a time. Holding a lock is how one thread tells other threads: “I’m changing this thing, don’t touch it right now.”Īcquire allows a thread to take ownership of a lock. Since race conditions caused by concurrent manipulation of shared mutable data are disastrous bugs - hard to discover, hard to reproduce, hard to debug - we need a way for concurrent modules that share memory to synchronize with each other.Ī lock is an abstraction that allows at most one thread to own it at a time. The correctness of a concurrent program should not depend on accidents of timing.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |