treusetvar.nim 470 B

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