Go Package sync: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
 
(23 intermediate revisions by the same user not shown)
Line 3: Line 3:


=Internal=
=Internal=
* [[Go_Language_Goroutines#Overview|Go Language Concurrency with Goroutines]]
* [[Go_Concurrency#Memory_Access_and_Thread_Synchronization_Primitives|Go Concurrency]]
* [[Go_Language_Modularization#sync|Standard library]]
* [[Go_Language_Modularization#sync|Standard library]]
* [[Go_Language_Goroutines#Synchronization_Constructs|Goroutines]]
* [[Go Package atomic|The <tt>atomic</tt> package]]
* [[Go Package atomic|The <tt>atomic</tt> package]]


=Overview=
=Overview=
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_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.
=<tt>WaitGroup</tt>=
 
Do not communicate by sharing memory. Instead, share memory by communicating.
 
Also see: {{Internal|Go_Concurrency#Programming_Models|Go Concurrency Programming Models}}
 
=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}}
 
=<span id='Thread_Synchronization_Primitives'></span>Execution Synchronization Primitives=
==<tt>WaitGroup</tt>==
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
=<tt>Mutex</tt>=
==<tt>Once</tt>==
{{Internal|Go Mutex#Overview|<tt>Mutex</tt>}}
=<tt>Once</tt>=
{{Internal|Go Once#Overview|<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