I have the following example below where two goroutine should be running in parallel. But if you check the output, the second goroutine only runs after the first goroutine completes. So, it's sequential.
Adding 2 processors: runtime.GOMAXPROCS(2) also didn't help. I'm running on a Mac pro with 8 cores and it is definitely not a hardware issue. So my question - Is Golang really parallel and how to make example below run in parallel?
Output:
Thread 1
Thread 1
…………....
Thread 1
Thread 1
Thread 2
Thread 2
…………....
Thread 2
Thread 2
Go code:
package main
import (
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(2)
go func() {
for i := 0; i < 100; i++ {
println("Thread 1")
//time.Sleep(time.Millisecond * 10)
}
}()
go func() {
for i := 0; i < 100; i++ {
println("Thread 2")
//time.Sleep(time.Millisecond * 10)
}
}()
time.Sleep(time.Second)
}
runtime.LockOSThread()
. Playground. But it doesn't guarantee that those threads are running on different cpu's. Maybe you can get ideas here: stackoverflow.com/a/19759235/694331 – ANisus