go


Is it possible collect goroutine result in the input order? [duplicate]


This question already has an answer here:
How to collect values from N goroutines executed in a specific order?
2 answers
I am thinking my result order should be the same as the input, is it possible to in go routine?
I implement like this:
package main
import "fmt"
import "time"
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
time.Sleep(time.Second)
results <- j * 2
}
}
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// Finally we collect all the results of the work.
// But somehow I want to keep the order
for a := 1; a <= 5; a++ {
fmt.Println(<-results) // actually, I want to make it 2, 4, 6, 8, 10
}
}
run it here
Must keep results order at worker func. goroutine run no ordered. set results must keep order.
package main
import "fmt"
import "time"
import "sync/atomic"
var pivot uint32 = 1
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
time.Sleep(time.Second)
for {
if n := atomic.LoadUint32(&pivot); int(n) == j {
results <- j * 2
atomic.AddUint32(&pivot, 1)
break
}
time.Sleep(time.Millisecond)
}
}
}
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// Finally we collect all the results of the work.
// But somehow I want to keep the order
for a := 1; a <= 5; a++ {
fmt.Println(<-results) // actually, I want to make it 2, 4, 6, 8, 10
}
}
run it here!

Related Links

Protobuf compilation with Go tools
Use one return value?
Why is there a race condition in this program?
Go concurrent access to pointers methods
How to identify Go program CPU usage hot spots?
Go: interface method call
How to modify a slice within for loop?
Reusing http connections in Golang
Why doesn't fmt.Scanf in Go wait for user input?
net/http ignoring system proxy settings
FileServer handler with some other HTTP handlers
Why does append() modify the provided slice? (See example)
Is this a closure example?
Copy a struct content into uint64 in Go
Go: Declare both a new variable and overwrite a variable from a higher scope, how?
Go's json decoder isn't working on the simplest possible input. Why?

Categories

HOME
phpword
spark-streaming
webdriver
battery
mstest
knitr
google-cloud-functions
cublas
entity-framework-core
tivoli
alsa
maxscript
python-multithreading
discrete-mathematics
pattern-matching
fabric.io
distribution
watson-conversation
3nf
flatpak
jersey-1.0
articulate-storyline
jquery-ias
dataflow-diagram
pchart
corpus
angularjs-components
ms-access-web-app
angular-translate
subclass
google-earth
dotspatial
papaparse
dsx-desktop
android-gridview
gnat-gps
instantiation
hibernate-search
imx6
latitude-longitude
loading
metaprogramming
p4merge
setup.py
parser-generator
fwrite
robomongo
rserve
remedy
context-switch
semantic-logging
oracleclient
java-websocket
objectscript
renaming
polyline
atlas
meteor-packages
omxplayer
flex-monkey
myspace
file-uri
x-tag
tracker-enabled-dbcontext
beanstalk
realsense
ascii-art
libvlc
video-player
canalyzer
typeconverter
matlab-deployment
zend-currency
sip-server
sql-scripts
human-computer-interface
alternate
vsx
qscrollarea
ecslidingviewcontroller
rfc5322
kogrid
route-provider
dolby-audio-api
bjyauthorize
curb
rmiregistry
mapping-by-code
jsr286
relative
filemerge
clrstoredprocedure
event-receiver
object-database
drupal-fivestar
zen
web-application-project
windows-identity
software-lifecycle
java1.4
business-model

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App