tuseafterdef.nim 671 B

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