Go Package sync: Difference between revisions

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


=Internal=
=Internal=
* [[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]]
* [[Go_Language_Concurrency#Overview|Go Concurrency]]


=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.  


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


* https://golang.org/pkg/sync
Also see: {{Internal|Go_Concurrency#Programming_Models|Go Concurrency Programming Models}}


==WaitGroup==
=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}}


* <tt>[https://golang.org/pkg/sync/#WaitGroup sync.WaitGroup]</tt>  
=<span id='Thread_Synchronization_Primitives'></span>Execution Synchronization Primitives=
 
==<tt>WaitGroup</tt>==
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.
{{Internal|Go WaitGroup#Overview|<tt>WaitGroup</tt>}}
 
==<tt>Once</tt>==
<blockquote style="background-color: AliceBlue; border: solid thin LightSteelBlue;">
{{Internal|Go Once#Overview|<tt>Once</tt>}}
:Using a <tt>waitGroup</tt> to wait for goroutines to finish before exiting <tt>main()</tt>: [https://github.com/NovaOrdis/playground/blob/master/go/concurrency/waitGroup.go playground/go/concurrency/waitGroup.go]
==<tt>Cond</tt>==
</blockquote>
{{Internal|Go Cond#Overview|<tt>Cond</tt>}}
 
==Mutex==
 
* <tt>[https://golang.org/pkg/sync/#Mutex sync.Mutex]</tt>
 
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.
 
Usage pattern (the curly braces are not necessary):
 
<pre>
var mutex sync.Mutex
 
...
 
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