Go Package sync: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(50 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
 
* https://pkg.go.dev/sync
* https://golang.org/pkg/sync


=Internal=
=Internal=
 
* [[Go_Concurrency#Memory_Access_and_Thread_Synchronization_Primitives|Go Concurrency]]
* [[Go Concepts - Standard Library#Packages|Standard Library]]
* [[Go_Language_Modularization#sync|Standard library]]
* [[Go Package atomic|The <tt>atomic</tt> package]]


=Overview=
=Overview=
The <code>sync</code> package provides [[Go_Concurrency#Memory_Access_and_Thread_Synchronization_Primitives|memory access and execution 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_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.


The "sync" package provides synchronization primitives, to be used when synchronization between goroutines is needed.
Do not communicate by sharing memory. Instead, share memory by communicating.
 
=Elements=
 
==WaitGroup==


* <tt>[https://golang.org/pkg/sync/#WaitGroup sync.WaitGroup]</tt>
Also see: {{Internal|Go_Concurrency#Programming_Models|Go Concurrency Programming Models}}


A <tt>WaitGroup</tt> is a counting semaphore that waits for a collection of goroutines to finish. The main goroutine calls <tt>Add()</tt> to set the number of goroutines to wait for. Then each of the goroutines runs and calls <tt>Done()</tt> when finished. At the same time, <tt>Wait()</tt> can be used to block until all goroutines have finished.
=Memory Access Synchronization Primitives=
==<tt>Mutex</tt>==
{{Internal|Go Mutex and RWMutex#Mutex|<tt>Mutex</tt>}}
==<tt>RWMutex</tt>==
{{Internal|Go Mutex and RWMutex#RWMutex|<tt>RWMutex</tt>}}
==<tt>Pool</tt>==
{{Internal|Go Pool|<tt>Pool</tt>}}
==<tt>Map</tt>==
{{External|https://pkg.go.dev/sync#Map}}


Example: [[Go_Recipes#waitGroup_example|<tt>WaitGroup</tt> example]].
=<span id='Thread_Synchronization_Primitives'></span>Execution Synchronization Primitives=
==<tt>WaitGroup</tt>==
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
==<tt>Once</tt>==
{{Internal|Go Once#Overview|<tt>Once</tt>}}
==<tt>Cond</tt>==
{{Internal|Go Cond#Overview|<tt>Cond</tt>}}

Latest revision as of 01:22, 21 January 2024

External

Internal

Overview

The sync package provides memory access and execution 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.

Also see:

Go Concurrency Programming Models

Memory Access Synchronization Primitives

Mutex

Mutex

RWMutex

RWMutex

Pool

Pool

Map

https://pkg.go.dev/sync#Map

Execution Synchronization Primitives

WaitGroup

WaitGroup

Once

Once

Cond

Cond