4 ms·
The way I would do it without seeing the implementation: - Atomic to track print order (name it counter). - Results slice stores items: {index, err, data}. -
by taosx 2y ago
The way I would do it without seeing the implementation:
- Atomic to track print order (name it counter).
- Results slice stores items: {index, err, data}.
- Goroutines: do work, store result, then loop-check if counter matches index to print (with tiny sleep).
Edit: Another solution would be to put printing loop outside with a channel to signal work finished but I don't know how I would avoid using nested loops, one for the items in the list and one to check if the result is back.
- binary132 2y agoThat is unnecessarily complicated, and you don’t need a sleep or atomics.... Just make an array of result channels, and loop over it awaiting each result. Each result has a producer either working, or waiting for the channel reader to consume it.