Skip to content

Instantly share code, notes, and snippets.

@physacco
Created March 18, 2013 03:02
Show Gist options
  • Save physacco/5184718 to your computer and use it in GitHub Desktop.
Save physacco/5184718 to your computer and use it in GitHub Desktop.
Test the effect of sync.Once.
// Test the effect of sync.Once.
// See another example: http://golang.org/pkg/sync/#Once
package main
import (
"fmt"
"sync"
"runtime"
)
var a int
var once sync.Once
func setup() {
a++
}
func doprint() {
once.Do(setup)
fmt.Println(a)
}
func main() {
runtime.GOMAXPROCS(2)
// these 3 goroutines runs on the same CPU
go doprint()
go doprint()
go doprint()
// prevent the process from exit
for { // infinite loop, consumes 100% CPU
}
}
// Output:
// 1
// 1
// 1
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment