I wrote a pool of workers, where the job is to receive an integer and return that number converted to string. However I faced a fatal error: all goroutines are asleep - deadlock! error. What am I doing wrong and how can I fix it?
如何修复工作者池死锁
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
douqiandai4327 2019-09-03 12:10关注I was able to replicate your issue and fix it by using a pointer to
masterinstead of a normal variable.Basically just change your
NewWorker()method to this:func (m *Master) NewWorker() { m.Workers = append(m.Workers, Worker{}) }Here's the output the program prints after the change:
0 1 2 3 4 5 6 7 8 9 10 . . .Reason:
Everytime you called
NewWorker()method, you weren't appending aworkerto the same master object. That's why the slice never got populated with 3 workers, as should've been the case.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报