title: Goroutines not Using Max CPU and Cores
body: <p>I'm realizing my very first <code>Golang</code> application and I'm having some issues on using <code>MAX CPU &amp; Cores</code> when using <code>GoRoutines</code> and I don't really know why.</p>
  
  <p>When using a tool such as <code>htop</code>, <code>CPU</code> isn't used at its max power and only <code>1..4 threads</code> are active at time. Also, all <code>cores</code> are active but they are around <code>25%-40%</code> utilization.</p>
  
  <p>I used:</p>
  
  <pre><code>func MaxParallelism() int {
      maxProcs := runtime.GOMAXPROCS(0)
      numCPU := runtime.NumCPU()
      if maxProcs &lt; numCPU {
          return maxProcs
      }
      return numCPU
  }
  </code></pre>
  
  <p>In order to get the number of <code>goroutines</code> to be instantiated.</p>
  
  <p>Here's How I set up the application:</p>
  
  <pre><code>//Common Channel for the goroutines
  tasks := make(chan *exec.Cmd, 64)
  
      //Spawning Max Cores Number goroutines (8)
      var wg sync.WaitGroup
      cores := MaxParallelism()
      for i := 0; i &lt; cores; i++ {
          wg.Add(1)
          go func(num int, w *sync.WaitGroup) {
              defer w.Done()
              var (
                  out []byte
                  err error
              )
              for cmd := range tasks {
                  out, err = cmd.Output()
                  if err != nil {
                      fmt.Printf("Can't get stdout:", err)
                  }
                  . . .
              }
          }(i, &amp;wg)
      }
  
      //Generate Tasks
      for i := 0; i &lt; 100000; i++ {
        tasks &lt;- exec.Command(cmd1, args...)
        tasks &lt;- exec.Command(cmd2, args...)
      }
  
  close(tasks)
  // wait for the workers to finish
  wg.Wait()
  </code></pre>
  
  <p>I share two screenshots of <code>htop</code> while executing the application</p>
  
  <p><a href="https://i.stack.imgur.com/CLu8A.png" rel="nofollow"><img src="https://i.stack.imgur.com/CLu8A.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/XM2OJ.png" rel="nofollow"><img src="https://i.stack.imgur.com/XM2OJ.png" alt="enter image description here"></a></p>
  
  <p>I don't know If it May help but I'm launching it through <code>Intellij Idea</code>.</p>
  
  <p><strong>How do I use Max CPU properly and Cores?</strong></p>
  
  <p>Thanks in advance.</p>