Concurrency Synchronization Techniques Provided In The sync Standard Package

The channel use cases article introduces many use cases in which channels are used to do data synchronizations among goroutines. In fact, channels are not the only synchronization techniques provided in Go. There are some other synchronization techniques supported by Go. For some specified circumstances, using the synchronization techniques other than channel are more efficient and readable than using channels. Below will introduce the synchronization techniques provided in the sync standard package.

The sync standard package provides several types which can be used to do synchronizations for some specialized circumstances and guarantee some specialized memory orders. For the specialized circumstances, these techniques are more efficient, and look cleaner, than the channel ways.

(Please note, to avoid abnormal behaviors, it is best never to copy the values of the types in the sync standard package.)

The sync.WaitGroup Type

Each sync.WaitGroup value maintains a counter internally. The initial value of the counter is zero.

The *WaitGroup type has three methods: Add(delta int), Done() and Wait().

For an addressable WaitGroup value wg,

Please note that wg.Add(delta), wg.Done() and wg.Wait() are shorthands of (&wg).Add(delta), (&wg).Done() and (&wg).Wait(), respectively.

Generally, a WaitGroup value is used for the scenario that one goroutine waits until all of several other goroutines finish their respective jobs. An example:
package main

import (
	"log"
	"math/rand"
	"sync"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())

	const N = 5
	var values [N]int32

	var wg sync.WaitGroup
	wg.Add(N)
	for i := 0; i < N; i++ {
		i := i
		go func() {
			values[i] = 50 + rand.Int31n(50)
			log.Println("Done:", i)
			wg.Done() // <=> wg.Add(-1)
		}()
	}

	wg.Wait()
	// All elements are guaranteed to be initialized now.
	log.Println("values:", values)
}
In the above example, the main goroutine waits until all other N goroutines have populated their respective element value in values array. Here is one possible output result:
Done: 4
Done: 1
Done: 3
Done: 0
Done: 2
values: [71 89 50 62 60]
We can split the only Add method call in the above example into multiple ones.
...
	var wg sync.WaitGroup
	for i := 0; i < N; i++ {
		wg.Add(1) // will be invoked N times
		i := i
		go func() {
			values[i] = 50 + rand.Int31n(50)
			wg.Done()
		}()
	}
...

The Wait method can be called in multiple goroutines. When the counter becomes zero, all of them will be notified, in a broadcast way.
func main() {
	rand.Seed(time.Now().UnixNano())

	const N = 5
	var values [N]int32

	var wgA, wgB sync.WaitGroup
	wgA.Add(N)
	wgB.Add(1)

	for i := 0; i < N; i++ {
		i := i
		go func() {
			wgB.Wait() // wait a notification
			log.Printf("values[%v]=%v \n", i, values[i])
			wgA.Done()
		}()
	}

	// The loop is guaranteed to finish before
	// any above wg.Wait calls returns.
	for i := 0; i < N; i++ {
		values[i] = 50 + rand.Int31n(50)
	}
	wgB.Done() // will make a broadcast notification
	wgA.Wait()
}

The sync.Once Type

A *sync.Once value has a Do(f func()) method, which takes a solo parameter with type func().

For an addressable Once value o, the method o.Do, which is a shorthand of (&o).Do, can be called multiple times, in multiple goroutines. The arguments of these o.Do calls should (but are not required to) be the same function value.

Among these o.Do method calls, only exact one argument function will be invoked. The invoked argument function is guaranteed to exit before any o.Do method call returns. In other words, the code in the invoked argument function is guaranteed to be executed before any o.Do method call returns.

Generally, a Once value is used to ensure that a piece of code will be executed exactly once in concurrent programming.

An example:
package main

import (
	"log"
	"sync"
)

func main() {
	log.SetFlags(0)

	x := 0
	doSomething := func() {
		x++
		log.Println("Hello")
	}

	var wg sync.WaitGroup
	var once sync.Once
	for i := 0; i < 5; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			once.Do(doSomething)
			log.Println("world!")
		}()
	}

	wg.Wait()
	log.Println("x =", x) // x = 1
}

In the above example, Hello will be printed once, but world! will be printed five times. And Hello is guaranteed to be printed before all five world!.

The sync.Mutex And sync.RWMutex Types

Both of the *sync.Mutex and *sync.RWMutex types implement the sync.Locker interface. So they both have two methods, Lock and Unlock, to prevent multiple data users from using a piece of data concurrently.

Besides the Lock and Unlock methods, the *RWMutex type has two other methods, RLock and RUnlock, to avoid some data users (either writers or readers) and one data writer using a piece of data at the same time but allow some data readers to access the piece of data at the same time.

(Note, here the terminologies data reader and data writer should not be interpreted from literal. They are just used for explanation purpose. A data reader might modify data and a data writer might only read data.)

A Mutex value is often called a mutual exclusion lock. A zero Mutex value is an unlocked mutex. A Mutex value can only be locked when it is in unlocked status. In other words, once an addressable Mutex value m is locked successfully (a.k.a., a m.Lock() method call returns), a new attempt by a goroutine to lock the Mutex value will make the goroutine enter blocking state, until the Mutex value is unlocked (through a m.Unlock() call).

Please note that m.Lock() and m.Unlock() are shorthands of (&m).Lock() and (&m).Unlock(), respectively.

An example of using sync.Mutex:
package main

import (
	"fmt"
	"runtime"
	"sync"
)

type Counter struct {
	m sync.Mutex
	n uint64
}

func (c *Counter) Value() uint64 {
	c.m.Lock()
	defer c.m.Unlock()
	return c.n
}

func (c *Counter) Increase(delta uint64) {
	c.m.Lock()
	c.n += delta
	c.m.Unlock()
}

func main() {
	var c Counter
	for i := 0; i < 100; i++ {
		go func() {
			for k := 0; k < 100; k++ {
				c.Increase(1)
			}
		}()
	}

	// The loop is just for demo purpose.
	for c.Value() < 10000 {
		runtime.Gosched()
	}
	fmt.Println(c.Value()) // 10000
}

In the above example, a Counter value uses a Mutex field to guarantee that the n field of the Counter value will be never used by multiple goroutines at the same time.

A RWMutex value is often called a reader+writer mutual exclusion lock. For an addressable RWMutex value m, data writers can acquire the write lock of m through m.Lock() method calls, and data readers can acquire the read lock of m through m.RLock() method calls. Method calls m.Unlock() and m.RUnlock() are used to release the write and read locks of m.

Please note that m.Lock(), m.Unlock(), m.RLock() and m.RUnlock() are shorthands of (&m).Lock(), (&m).Unlock(), (&m).RLock() and (&m).RUnlock(), respectively.

For an addressable RWMutex value m, the following rules exist.

The last two rules are to ensure both readers and writers have chances to acquire locks.

The type of the m field of the Counter type in the last example can be changed to sync.RWMutex, as the following code shows, to a better performance.
...
type Counter struct {
	//m sync.Mutex
	m sync.RWMutex
	n uint64
}

func (c *Counter) Value() uint64 {
	//c.m.Lock()
	//defer c.m.Unlock()
	c.m.RLock()
	defer c.m.RUnlock()
	return c.n
}
...

By the last two rules mentioned above, the following program is very possible to output abdc.
package main

import (
	"fmt"
	"time"
	"sync"
)

func main() {
	var m sync.RWMutex
	go func() {
		m.RLock()
		fmt.Print("a")
		time.Sleep(time.Second)
		m.RUnlock()
	}()
	go func() {
		time.Sleep(time.Second * 1 / 4)
		m.Lock()
		fmt.Print("b")
		time.Sleep(time.Second)
		m.Unlock()
	}()
	go func() {
		time.Sleep(time.Second * 2 / 4)
		m.Lock()
		fmt.Print("c")
		m.Unlock()
	}()
	go func () {
		time.Sleep(time.Second * 3 / 4)
		m.RLock()
		fmt.Print("d")
		m.RUnlock()
	}()
	time.Sleep(time.Second * 3)
	fmt.Println()
}

Please note, the above example is only for explanation purpose. It uses time.Sleep calls to do concurrency synchronizations, which is a bad practice for production code.

sync.Mutex and sync.RWMutex values can also be used to make notifications, though there are many other better ways to do the same job. Here is an example which makes a notification by using a sync.Mutex value.
package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	var m sync.Mutex
	m.Lock()
	go func() {
		time.Sleep(time.Second)
		fmt.Println("Hi")
		m.Unlock() // make a notification
	}()
	m.Lock() // wait to be notified
	fmt.Println("Bye")
}

In the above example, the text Hi is guaranteed to be printed before the text Bye. About the memory order guarantees made by sync.Mutex and sync.RWMutex values, please read memory order guarantees in Go.

The sync.Cond Type

The sync.Cond type provides an efficient way to do notifications among goroutines.

Each sync.Cond value holds a sync.Locker field with name L. The field value is often a value of type *sync.Mutex or *sync.RWMutex.

The *sync.Cond type has three methods, Wait(), Signal() and Broadcast().

Each sync.Cond value also maintains a FIFO (first in first out) waiting goroutine queue. For an addressable sync.Cond value c,

Please note that c.Wait(), c.Signal() and c.Broadcast() are shorthands of (&c).Wait(), (&c).Signal() and (&c).Broadcast(), respectively.

c.Signal() and c.Broadcast() are often used to notify the status of a condition is changed, Generally, c.Wait() should be called in a loop of checking whether or not a condition has got satisfied.

In an idiomatic sync.Cond use case, generally, one goroutine waits for changes of a certain condition, and some other goroutines change the condition and send notifications. Here is an example:
package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())

	const N = 10
	var values [N]string

	cond := sync.NewCond(&sync.Mutex{})
	cond.L.Lock()

	for i := 0; i < N; i++ {
		d := time.Second * time.Duration(rand.Intn(10)) / 10
		go func(i int) {
			time.Sleep(d) // simulate a workload
			cond.L.Lock() // changes must be made when cond.L is locked
			values[i] = string('a' + i)
			cond.Signal() // called when cond.L lock is acquired
			cond.L.Unlock()
			// The above "cond.Signal()" line can also be put here.
			//cond.Signal() // called when cond.L lock is released.
		}(i)
	}

	// This function must be called when cond.L is locked.
	checkCondition := func() bool {
		fmt.Println(values)
		for i := 0; i < N; i++ {
			if values[i] == "" {
				return false
			}
		}
		return true
	}
	for !checkCondition() {
		cond.Wait() // must be called when cond.L is locked
	}
	cond.L.Unlock()
}
One possible output:
[         ]
[     f    ]
[  c   f    ]
[  c   f  h  ]
[ b c   f  h  ]
[a b c   f  h  j]
[a b c   f g h i j]
[a b c  e f g h i j]
[a b c d e f g h i j]

For there is only one goroutine (the main goroutine) waiting to be unblocked in this example, the cond.Signal() call can be replaced with cond.Broadcast(). As the comments suggest, cond.Signal() and cond.Broadcast() are not required to be called when cond.L is locked.

To avoid data races, each of the ten parts of the user defined condition should only be modified when cond.L is locked. The checkCondition function and the cond.Wait method should be also called when cond.L is locked.

In fact, for the above specified example, the cond.L field can also be a *sync.RWMutex value, and each of the ten parts of the user defined condition can be modified when the read lock of cond.L is held, just as the following code shows:

...
	cond := sync.NewCond(&sync.RWMutex{})
	cond.L.Lock()

	for i := 0; i < N; i++ {
		d := time.Second * time.Duration(rand.Intn(10)) / 10
		go func(i int) {
			time.Sleep(d)
			cond.L.(*sync.RWMutex).Lock()
			values[i] = string('a' + i)
			cond.L.(*sync.RWMutex).Unlock()
			cond.Signal()
		}(i)
	}
...

In the above example, the sync.RWMutex value is used unusually. Its read lock is held by some goroutines which modify array elements, and its write lock is used by the main goroutine to read array elements.

The user defined condition monitored by a Cond value can be a void. For such cases, the Cond value is used for notifications purely. For example, the following program will print abc or bac.
package main

import (
	"fmt"
	"sync"
)

func main() {
	wg := sync.WaitGroup{}
	wg.Add(1)
	cond := sync.NewCond(&sync.Mutex{})
	cond.L.Lock()
	go func() {
		cond.L.Lock()
		go func() {
			cond.L.Lock()
			cond.Broadcast()
			cond.L.Unlock()
		}()
		cond.Wait()
		fmt.Print("a")
		cond.L.Unlock()
		wg.Done()
	}()
	cond.Wait()
	fmt.Print("b")
	cond.L.Unlock()
	wg.Wait()
	fmt.Println("c")
}

If it needs, multiple sync.Cond values can share the same sync.Locker. However, such cases are rare in practice.


The Go 101 project is hosted on both Github and Gitlab. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

Support Go 101 by playing Tapir's games. Cryptocurrency donations are also welcome:
Bitcoin: 1xucQbv5jujFPPwhyg395ri5yV71hx9g9
Ethereum: 0x5dc4aa2c2bbfaadae373dadcfca11b3358912212