Created
January 27, 2017 15:09
-
-
Save skelterjohn/672fdcfed49aadd14834baff99de6424 to your computer and use it in GitHub Desktop.
cooperative scheduling with cpu intensive tasks, GOMAXPROCS=1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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