Are Go locks/mutex not the same as ordinary locks/mutexes?

205 views
Skip to first unread message

Roger Alsing

unread,
Nov 29, 2016, 12:51:16 PM11/29/16
to golang-nuts
Coming from C++/C# background where locks/mutexes are considered evil due to blocking threads.
Due to how the Go goroutine scheduler works, are the Go counterpart of those primitives "different"?

Should I see the Go variants of these primitives more like yield points where the execution of a goroutine yields to let other goroutines run.
And thus not resulting in the same resource code/weight as the primitives has in other languages?

Ian Lance Taylor

unread,
Nov 29, 2016, 1:10:49 PM11/29/16
to Roger Alsing, golang-nuts
Yes. A Go sync.Mutex blocks a goroutine, not a thread. Goroutines
are lighter weight than threads--they are basically just a stack.

Ian

adon...@google.com

unread,
Nov 30, 2016, 9:37:04 AM11/30/16
to golang-nuts, roger...@gmail.com
To elaborate: the key difference as it relates to your question is not really the size of the goroutine data structure or stack, but that a goroutine yield operation is usually handled entirely in user mode by the Go scheduler, avoiding execution of the the kernel's thread scheduler and a heavyweight context switch.

Reply all
Reply to author
Forward
0 new messages