Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: Why are goroutines not running in parallel?

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)
}
like image 628
PARUS Avatar asked Oct 12 '25 11:10

PARUS


1 Answers

In order to understand your program is running parallel or concurrently using goroutine is print the different value in sequence. From this article : Concurrency, Goroutines and GOMAXPROCS .

Your code can't express enough to represent a parallel call. Please see the code below.

package main

import (
    "fmt"
    "runtime"
    "sync"
)

func main() {
    runtime.GOMAXPROCS(2)

    var wg sync.WaitGroup
    wg.Add(2)

    fmt.Println("Starting Go Routines")
    go func() {
        defer wg.Done()

        //time.Sleep(1 * time.Microsecond)
        for char := 'a'; char < 'a'+26; char++ {
            fmt.Printf("%c ", char)
        }
    }()

    go func() {
        defer wg.Done()

        for number := 1; number < 27; number++ {
            fmt.Printf("%d ", number)
        }
    }()

    fmt.Println("Waiting To Finish")
    wg.Wait()

    fmt.Println("\nTerminating Program")
}

As you can see from the above code print the different value sequence using for loop.

The output would be different if you comment the runtime.GOMAXPROCS(2) and uncomment the line on time.Sleep(1 * time.Microsecond).

like image 186
Gujarat Santana Avatar answered Oct 15 '25 11:10

Gujarat Santana