Skip to content

Instantly share code, notes, and snippets.

@shreyansb
Created September 7, 2012 14:05
Show Gist options
  • Save shreyansb/3666521 to your computer and use it in GitHub Desktop.
Save shreyansb/3666521 to your computer and use it in GitHub Desktop.
this will block the cpu entirely
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
// THIS is totally required; without it, you're blocking the CPU regardless
// of whether you use LockOSThread or not.
runtime.GOMAXPROCS(4)
// arbitrary CPU-blocking function
go cpublock()
go cpublock()
go cpublock()
go cpublock()
for {
fmt.Println("hey")
time.Sleep(time.Second)
}
}
func cpublock() {
x := 0
for {
x += 1
}
}
@shreyansb
Copy link
Author

this will print 'hey' once, then when the time.Sleep yields to the scheduler, the last cpublock goroutine will start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment