2016-11-16 21:30


  • concurrency

I am trying to parallelize an operation in golang and save the results in a manner that I can iterate over to sum up afterwords.

I have managed to set up the parameters so that no deadlock occurs, and I have confirmed that the operations are working and being saved correctly within the function. When I iterate over the Slice of my struct and try and sum up the results of the operation, they all remain 0. I have tried passing by reference, with pointers, and with channels (causes deadlock).

I have only found this example for help: But this seems outdated now, as Vector as been deprecated? I also have not found any references to the way this function (in the example) was constructed (with the func (u Vector) before the name). I tried replacing this with a Slice but got compile time errors.

Any help would be very appreciated. Here is the key parts of my code:

type job struct {
  a int
  b int
  result *big.Int

func choose(jobs []Job, c chan int) {
  temp := new(big.Int)
  for _,job := range jobs {
    job.result = //perform operation on job.a and job.b
  c <- 1

func main() {
  num := 100 //can be very large (why we need big.Int)
  n := num
  k := 0
  const numCPU = 6 //runtime.NumCPU
  count := new(big.Int)

  // create a 2d slice of jobs, one for each core
  jobs := make([][]Job, numCPU)

  for (float64(k) <= math.Ceil(float64(num / 2))) {
    // add one job to each core, alternating so that
    // job set is similar in difficulty
    for i := 0; i < numCPU; i++ {
      if !(float64(k) <= math.Ceil(float64(num / 2))) {
      jobs[i] = append(jobs[i], Job{n, k, new(big.Int)})
      n -= 1
      k += 1

  c := make(chan int, numCPU)
  for i := 0; i < numCPU; i++ {
    go choose(jobs[i], c)

  // drain the channel
  for i := 0; i < numCPU; i++ {
  // computations are done

 for i := range jobs {
    for _,job := range jobs[i] {
      count.Add(count, job.result)


Here is the code running on the go playground

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享