Go Package sync: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 10: Line 10:
The <code>sync</code> package provides [[Go_Language_Goroutines#Synchronization|synchronization]] primitives to use when synchronization between [[Go_Language_Goroutines#Synchronization|goroutines]] is necessary.
The <code>sync</code> package provides [[Go_Language_Goroutines#Synchronization|synchronization]] primitives to use when synchronization between [[Go_Language_Goroutines#Synchronization|goroutines]] is necessary.


However, other than the <code>[[Go_Once#Overview|Once]]</code> and <code>[[Go_WaitGroup#Overview|WaitGroup]]</code> types, most <code>sync</code> primitives are intended for use by low-level library routines. Higher level synchronization is better done via [[Go_Language_Channels#Overview|channels]] and communication.
However, other than the <code>[[Go_Once#Overview|Once]]</code> and <code>[[Go_WaitGroup#Overview|WaitGroup]]</code> types, most <code>sync</code> primitives are intended for use by low-level library routines. Higher level synchronization is better done via [[Go_Language_Channels#Overview|channels]] and communication.
 
Regarding Mutexes, the <code>sync</code> package implements them, but the language designers expressed their hope that Go programming style will encourage people to try higher-level techniques. In particular, programmers should structure their program so that only one goroutine at a time is ever responsible for a particular piece of data.
 
Do not communicate by sharing memory. Instead, share memory by communicating.


=Memory Access Synchronization Primitives=
=Memory Access Synchronization Primitives=

Revision as of 23:21, 15 January 2024

External

Internal

Overview

The sync package provides synchronization primitives to use when synchronization between goroutines is necessary.

However, other than the Once and WaitGroup types, most sync primitives are intended for use by low-level library routines. Higher level synchronization is better done via channels and communication.

Regarding Mutexes, the sync package implements them, but the language designers expressed their hope that Go programming style will encourage people to try higher-level techniques. In particular, programmers should structure their program so that only one goroutine at a time is ever responsible for a particular piece of data.

Do not communicate by sharing memory. Instead, share memory by communicating.

Memory Access Synchronization Primitives

Thread Synchronization Primitives

WaitGroup

WaitGroup

Mutex

Mutex

RWMutex

RWMutex

Once

Once

Cond

Cond