Go Package sync: Difference between revisions

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


=Internal=
=Internal=
* [[Go_Language_Concurrency#Overview|Go Concurrency]]
* [[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>=
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}


=TO DISTRIBUTE=
Do not communicate by sharing memory. Instead, share memory by communicating.
<FONT COLOR=darkkhaki>


==Mutex==
Also see: {{Internal|Go_Concurrency#Programming_Models|Go Concurrency Programming Models}}


* <tt>[https://golang.org/pkg/sync/#Mutex sync.Mutex]</tt>
=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}}


A Mutex is a mutual exclusion lock. Only one goroutine can enter the critical section at a time. Not until the call to the <tt>Unlock()</tt> function issued can another goroutine enter the critical section.
=<span id='Thread_Synchronization_Primitives'></span>Execution Synchronization Primitives=
 
==<tt>WaitGroup</tt>==
Usage pattern (the curly braces are not necessary):
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
 
==<tt>Once</tt>==
<pre>
{{Internal|Go Once#Overview|<tt>Once</tt>}}
var mutex sync.Mutex
==<tt>Cond</tt>==
 
{{Internal|Go Cond#Overview|<tt>Cond</tt>}}
...
 
mutex.Lock() {
    // do something in a mutual exclusion mode
}
 
mutex.Unlock()
</pre>
</FONT>

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