liveBook · Manning

WriteUnlock

As explained in Learn Concurrent Programming with Go

WriteUnlock is a function that releases the exclusive write side of the book's readers-writer mutex implementation.

WriteUnlock releases the exclusive writer side of a readers–writer mutex after a writer finishes its critical section. It unlocks globalLock, allowing a blocked writer or the waiting readers to proceed. section 4.2.2

The last step out of the writer’s entrance

Imagine shared data as a room. WriteLock closes the writer’s entrance while the writer works; WriteUnlock opens that entrance again by releasing globalLock. Readers can then acquire the read side when no writer holds the lock. section 4.2.2

Listing 4.14 Implementation of the ReadUnlock() function
func (rw *ReadWriteMutex) ReadUnlock() {
    rw.readersLock.Lock()                  #1
    rw.readersCounter--                    #2
    if rw.readersCounter == 0 {
        rw.globalLock.Unlock()             #3
    }
    rw.readersLock.Unlock()                #4
}

func (rw *ReadWriteMutex) WriteUnlock() {
    rw.globalLock.Unlock()                 #5
}
#1 Synchronizes access so that only one goroutine is allowed at any time
#2 The reader goroutine decrements readersCounter by 1.
#3 If the reader goroutine is the last one out, it unlocks the global lock.
#4 Synchronizes access so that only one goroutine is allowed at any time
#5 The writer goroutine, finishing its critical section, releases the global lock.

The implementation pairs WriteUnlock with WriteLock: the writer releases globalLock after completing its critical section. section 4.2.2

How Learn Concurrent Programming with Go covers it

How deep it goes
Shows the single unlock operation and its pairing with WriteLock, without descending into runtime internals.
Examples
Uses a shared room analogy and a Go method that calls globalLock.Unlock.
What it assumes
Assumes mutexes, goroutines, critical sections, and the complementary WriteLock operation.
What you can do after
Apply it by releasing globalLock when a writer finishes its critical section.
How it connects
Connects to readers–writer mutexes, WriteLock, ReadLock, ReadUnlock, goroutines, mutexes, and critical sections.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.