treusetvar.nim 453 B

1234567891011121314151617181920212223242526272829
  1. discard """
  2. outputsub: "65"
  3. """
  4. import locks
  5. type
  6. MarkerObj = object
  7. lock: Lock
  8. counter: int
  9. Marker = ptr MarkerObj
  10. const
  11. ThreadsCount = 65
  12. proc worker(p: Marker) {.thread.} =
  13. acquire(p.lock)
  14. inc(p.counter)
  15. release(p.lock)
  16. var p = cast[Marker](allocShared0(sizeof(MarkerObj)))
  17. initLock(p.lock)
  18. for i in 0..(ThreadsCount - 1):
  19. var thread: Thread[Marker]
  20. createThread(thread, worker, p)
  21. joinThread(thread)
  22. echo p.counter