Concurrent computing: Difference between revisions
Content added Content deleted
m (→{{header|Vlang}}: Using vfmt on the code) |
|||
Line 2,093: | Line 2,093: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
Porting of Go code |
Porting of Go code |
||
<lang |
<lang go>import time |
||
import rand |
import rand |
||
import rand.pcg32 |
import rand.pcg32 |
||
Line 2,099: | Line 2,099: | ||
fn main() { |
fn main() { |
||
words := ['Enjoy', 'Rosetta', 'Code'] |
|||
seed_u64 := u64(time.now().unix_time_milli()) |
|||
q := chan string{} |
|||
for i, w in words { |
|||
go fn (q chan string, w string, seed_u64 u64) { |
|||
mut rng := pcg32.PCG32RNG{} |
|||
time_seed := seed.time_seed_array(2) |
|||
seed_arr := [u32(seed_u64), u32(seed_u64 >> 32), time_seed[0], time_seed[1]] |
|||
rng.seed(seed_arr) |
|||
time.sleep(time.Duration(rng.i64n(1_000_000_000))) |
|||
q <- w |
|||
}(q, w, seed_u64 + u64(i)) |
|||
} |
|||
} |
|||
for _ in 0 .. words.len { |
|||
println(<-q) |
|||
} |
|||
} |
|||
}</lang> |
}</lang> |
||
===More Idiomatic version=== |
===More Idiomatic version=== |
||
<lang |
<lang go>import time |
||
import rand |
import rand |
||
import rand.pcg32 |
import rand.pcg32 |
||
Line 2,124: | Line 2,124: | ||
fn main() { |
fn main() { |
||
words := ['Enjoy', 'Rosetta', 'Code'] |
|||
mut threads := []thread{} // mutable array to hold the id of the thread |
|||
for w in words { |
|||
threads << go fn (w string) { // record the thread |
|||
mut rng := pcg32.PCG32RNG{} |
|||
time_seed := seed.time_seed_array(4) // the time derived array to seed the random generator |
|||
rng.seed(time_seed) |
|||
time.sleep(time.Duration(rng.i64n(1_000_000_000))) |
|||
println(w) |
|||
}(w) |
|||
} |
|||
} |
|||
threads.wait() // join the thread waiting. wait() is defined for threads and arrays of threads |
|||
}</lang> |
}</lang> |
||
{{out}}<pre>Code |
{{out}}<pre>Code |