Understanding goroutines

前端 未结 4 1567
情深已故
情深已故 2020-12-04 19:46

I\'m trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program:

package main

import          


        
4条回答
  •  一整个雨季
    2020-12-04 20:05

    No sure, but I think that inc_x is hogging the CPU. Since there's no IO it doesn't release control.

    I found two things that solved that. One was to call runtime.GOMAXPROCS(2) at the beginning of the program and then it'll work since now there are two threads serving goroutings. The other is to insert time.Sleep(1) after incrementing x.

提交回复
热议问题