tuseafterdef.nim 688 B

1234567891011121314151617181920212223242526272829303132333435
  1. discard """
  2. matrix: "--mm:refc"
  3. disabled: true
  4. errormsg: "(k)..(k) not disjoint from (k)..(k)"
  5. line: 24
  6. action: compile
  7. """
  8. # bug #1597
  9. import strutils, math, threadpool
  10. type
  11. BoxedFloat = object
  12. value: float
  13. proc term(k: float): ptr BoxedFloat =
  14. var temp = 4 * math.pow(-1, k) / (2*k + 1)
  15. result = cast[ptr BoxedFloat](allocShared(sizeof(BoxedFloat)))
  16. result.value = temp
  17. proc pi(n: int): float =
  18. var ch = newSeq[ptr BoxedFloat](n+1)
  19. parallel:
  20. for k in 0..ch.high:
  21. ch[k] = (spawn term(float(k)))
  22. assert ch[k] != nil
  23. for k in 0..ch.high:
  24. var temp = ch[k][].value
  25. result += temp
  26. deallocShared(ch[k])
  27. echo formatFloat(pi(5000))