tdisjoint_slice1.nim 857 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. discard """
  2. outputsub: "EVEN 28"
  3. """
  4. import threadpool, locks
  5. block one:
  6. proc odd(a: int) = echo "ODD ", a
  7. proc even(a: int) = echo "EVEN ", a
  8. proc main() =
  9. var a: array[0..30, int]
  10. for i in low(a)..high(a): a[i] = i
  11. parallel:
  12. var i = 0
  13. while i <= 29:
  14. spawn even(a[i])
  15. spawn odd(a[i+1])
  16. inc i, 2
  17. # is correct here
  18. main()
  19. block two:
  20. var echoLock: Lock
  21. initLock echoLock
  22. proc f(a: openArray[int]) =
  23. for x in a:
  24. withLock echoLock:
  25. echo x
  26. proc f(a: int) =
  27. withLock echoLock:
  28. echo a
  29. proc main() =
  30. var a: array[0..9, int] = [0,1,2,3,4,5,6,7,8,9]
  31. parallel:
  32. spawn f(a[0..2])
  33. #spawn f(a[16..30])
  34. var i = 3
  35. while i <= 8:
  36. spawn f(a[i])
  37. spawn f(a[i+1])
  38. inc i, 2
  39. # is correct here
  40. main()