Skip to content

Instantly share code, notes, and snippets.

@skelterjohn
Created January 27, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skelterjohn/672fdcfed49aadd14834baff99de6424 to your computer and use it in GitHub Desktop.
Save skelterjohn/672fdcfed49aadd14834baff99de6424 to your computer and use it in GitHub Desktop.
cooperative scheduling with cpu intensive tasks, GOMAXPROCS=1
package main
import (
"encoding/json"
"fmt"
"runtime"
"strings"
"sync"
)
const testBytes = `{ "Test": "value" }`
type Message struct {
Test string
}
var wg sync.WaitGroup
func cpuIntensive(p *Message, i int) {
fmt.Printf("Started intensive thing %d\n", i)
for i := int64(1); i <= 1000000; i++ {
json.NewDecoder(strings.NewReader(testBytes)).Decode(p)
}
fmt.Printf("Done intensive thing %d\n", i)
wg.Done()
}
func main() {
runtime.GOMAXPROCS(1)
x := Message{}
wg.Add(2)
go cpuIntensive(&x, 1)
go cpuIntensive(&x, 2)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment