tmanyjoin.nim 497 B

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