This commit is contained in:
Eugene Kabanov 2017-03-23 17:13:38 +02:00 • committed by Andreas Rumpf
commit 254fbcc548
4 changed files with 84 additions and 19 deletions

View file

@ -0,0 +1,30 @@
discard """
outputsub: "129"
"""
import os, locks
type
MarkerObj = object
lock: Lock
counter: int
Marker = ptr MarkerObj
const
ThreadsCount = 129
SleepTime = 1000
proc worker(p: Marker) {.thread.} =
acquire(p.lock)
inc(p.counter)
release(p.lock)
sleep(SleepTime)
var p = cast[Marker](allocShared0(sizeof(MarkerObj)))
initLock(p.lock)
var ts = newSeq[Thread[Marker]](ThreadsCount)
for i in 0..<ts.len:
createThread(ts[i], worker, p)
joinThreads(ts)
echo p.counter

View file

@ -0,0 +1,28 @@
discard """
outputsub: "129"
"""
import os, locks
type
MarkerObj = object
lock: Lock
counter: int
Marker = ptr MarkerObj
const
ThreadsCount = 129
proc worker(p: Marker) {.thread.} =
acquire(p.lock)
inc(p.counter)
release(p.lock)
var p = cast[Marker](allocShared0(sizeof(MarkerObj)))
initLock(p.lock)
for i in 0..(ThreadsCount - 1):
var thread: Thread[Marker]
createThread(thread, worker, p)
joinThread(thread)
echo p.counter