tthread.nim 983 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. discard """
  2. cmd: "nim cpp --gc:arc --threads:on $file"
  3. output: '''ok1
  4. ok2
  5. destroyed
  6. destroyed
  7. destroyed
  8. '''
  9. """
  10. import threadpool, os
  11. type
  12. MyObj = object
  13. p: int
  14. MyObjRef = ref MyObj
  15. proc `=destroy`(x: var MyObj) =
  16. if x.p != 0:
  17. echo "destroyed"
  18. proc thread1(): string =
  19. os.sleep(1000)
  20. return "ok1"
  21. proc thread2(): ref string =
  22. os.sleep(1000)
  23. new(result)
  24. result[] = "ok2"
  25. proc thread3(): ref MyObj =
  26. os.sleep(1000)
  27. new(result)
  28. result[].p = 2
  29. var fv1 = spawn thread1()
  30. var fv2 = spawn thread2()
  31. var fv3 = spawn thread3()
  32. sync()
  33. echo ^fv1
  34. echo (^fv2)[]
  35. proc thread4(x: MyObjRef): MyObjRef {.nosinks.} =
  36. os.sleep(1000)
  37. result = x
  38. proc thread5(x: sink MyObjRef): MyObjRef =
  39. os.sleep(1000)
  40. result = x
  41. proc ref_forwarding_test =
  42. var x = new(MyObj)
  43. x[].p = 2
  44. var y = spawn thread4(x)
  45. proc ref_sink_forwarding_test =
  46. var x = new(MyObj)
  47. x[].p = 2
  48. var y = spawn thread5(x)
  49. ref_forwarding_test()
  50. ref_sink_forwarding_test()
  51. sync()