I have never given a proper look at threading on nim. Is it safe to assume this
code won't produce any race conditions or undefined behavior?
import random, threadpool
type
RandGen = ref object
rands: seq[Rand]
Rand = object
list: seq[int]
var gen: RandGen
proc randomize(i: int) =
for _ in 0 ..< 10: add(gen.rands[i].list, rand(10))
proc main =
randomize()
gen = RandGen(rands: @[Rand(), Rand(), Rand()])
for i in 0 ..< len(gen.rands):
proc safeRandomize(i: int) =
{.gcsafe.}: randomize(i)
spawn safeRandomize(i)
sync()
for rand in gen.rands:
echo rand.list
main()
Run
- Re: I'm fed with Nim , I guess. Aiesha_Nazarothi
- Re: I'm fed with Nim , I guess. Araq
- Re: I'm fed with Nim , I guess. Aiesha_Nazarothi
- Re: I'm fed with Nim , I guess. Aiesha_Nazarothi
- Re: I'm fed with Nim , I guess. yglukhov
- Re: I'm fed with Nim , I guess. Aiesha_Nazarothi
- Re: I'm fed with Nim , I guess. yglukhov
- Re: I'm fed with Nim , I guess. Aiesha_Nazarothi
- Re: I'm fed with Nim , I guess. mikra
- Re: I'm fed with Nim , I guess. moerm
- Re: I'm fed with Nim , I guess. Arrrrrrrrr
- Re: I'm fed with Nim , I guess. yglukhov
- Re: I'm fed with Nim , I guess. boia01
- Re: I'm fed with Nim , I guess. mratsim
- Re: I'm fed with Nim , I guess. moerm
- Re: I'm fed with Nim , I guess. Aiesha_Nazarothi
