Quantcast
Channel: User Norbert - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by Norbert for Mutex not locking

$
0
0

You might want to write this a bit different:

You start by locking, which is not the most common way to use a lock: Everything is locked unless you unlock it.

A more common way is:

package mainimport ("fmt""sync")type Box struct {    Stuff int    mu    sync.Mutex}var box Boxfunc init() {    box = Box{}}func (box *Box) add() {    fmt.Println("add()")    box.mu.Lock()    box.Stuff++    box.mu.Unlock()}...

Scoping your locks to the moment of change and not to the complete app.


Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>