tmanyjoin.nim 513 B

1234567891011121314151617181920212223242526272829303132
  1. discard """
  2. disabled: i386
  3. outputsub: "129"
  4. """
  5. import os, locks
  6. type
  7. MarkerObj = object
  8. lock: Lock
  9. counter: int
  10. Marker = ptr MarkerObj
  11. const
  12. ThreadsCount = 129
  13. SleepTime = 250
  14. proc worker(p: Marker) {.thread.} =
  15. acquire(p.lock)
  16. inc(p.counter)
  17. release(p.lock)
  18. sleep(SleepTime)
  19. var p = cast[Marker](allocShared0(sizeof(MarkerObj)))
  20. initLock(p.lock)
  21. var ts = newSeq[Thread[Marker]](ThreadsCount)
  22. for i in 0..<ts.len:
  23. createThread(ts[i], worker, p)
  24. joinThreads(ts)
  25. echo p.counter