[ORC] replace threadpool with createThread in the runnableExamples (#20106)

replace threadpool with createThreads in the runnableExamples
This commit is contained in:
ringabout 2022-08-01 08:02:23 +08:00 • committed by GitHub
commit a8590d6707
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -178,24 +178,31 @@ proc skipRandomNumbers*(s: var Rand) =
## **See also:** ## **See also:**
## * `next proc<#next,Rand>`_ ## * `next proc<#next,Rand>`_
runnableExamples("--threads:on"): runnableExamples("--threads:on"):
import std/[random, threadpool] import std/random
const spawns = 4
const numbers = 100000 const numbers = 100000
proc randomSum(r: Rand): int = var
var r = r thr: array[0..3, Thread[(Rand, int)]]
vals: array[0..3, int]
proc randomSum(params: tuple[r: Rand, index: int]) {.thread.} =
var r = params.r
for i in 1..numbers: for i in 1..numbers:
result += r.rand(0..10) vals[params.index] += r.rand(0..10)
var r = initRand(2019) var r = initRand(2019)
var vals: array[spawns, FlowVar[int]] for i in 0..<thr.len:
for val in vals.mitems: createThread(thr[i], randomSum, (r, i))
val = spawn randomSum(r)
r.skipRandomNumbers() r.skipRandomNumbers()
joinThreads(thr)
for val in vals: for val in vals:
doAssert abs(^val - numbers * 5) / numbers < 0.1 doAssert abs(val - numbers * 5) / numbers < 0.1
doAssert vals == [501737, 497901, 500683, 500157]
when defined(js): when defined(js):
const helper = [0xbeac0467u32, 0xd86b048bu32] const helper = [0xbeac0467u32, 0xd86b048bu32]