Go Package sync: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Tag: Reverted
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 8: Line 8:


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


Do not communicate by sharing memory. Instead, share memory by communicating.
Do not communicate by sharing memory. Instead, share memory by communicating.
Line 19: Line 19:
==<tt>RWMutex</tt>==
==<tt>RWMutex</tt>==
{{Internal|Go Mutex and RWMutex#RWMutex|<tt>RWMutex</tt>}}
{{Internal|Go Mutex and RWMutex#RWMutex|<tt>RWMutex</tt>}}
=Thread Synchronization Primitives=
==<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>==
==<tt>WaitGroup</tt>==
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
Line 26: Line 31:
==<tt>Cond</tt>==
==<tt>Cond</tt>==
{{Internal|Go Cond#Overview|<tt>Cond</tt>}}
{{Internal|Go Cond#Overview|<tt>Cond</tt>}}
=Patterns=
==Guarding the Internal State of a <tt>struct</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