tthreadvars.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. discard """
  2. output: '''
  3. 10
  4. 1111
  5. 1222
  6. 3030303
  7. 3060606
  8. 6060606
  9. 6121212
  10. 3030903
  11. 3061206
  12. 3031503
  13. 3061806
  14. 5050505
  15. 5101010
  16. '''
  17. """
  18. import typetraits
  19. var tls1 {.threadvar.}: int
  20. var g0: int
  21. var g1 {.global.}: int
  22. proc customInc(x: var int, delta: int) =
  23. x += delta
  24. customInc(tls1, 10)
  25. echo tls1
  26. proc nonGenericProc: int =
  27. var local: int
  28. var nonGenericTls {.threadvar.}: int
  29. var nonGenericGlobal {.global.}: int
  30. var nonGenericMixedPragmas {.global, threadvar.}: int
  31. customInc local, 1000
  32. customInc nonGenericTls, 1
  33. customInc nonGenericGlobal, 10
  34. customInc nonGenericMixedPragmas, 100
  35. return local + nonGenericTls + nonGenericGlobal + nonGenericMixedPragmas
  36. proc genericProc(T: typedesc): int =
  37. var local: int
  38. var genericTls {.threadvar.}: int
  39. var genericGlobal {.global.}: int
  40. var genericMixedPragmas {.global, threadvar.}: int
  41. customInc local, T.name.len * 1000000
  42. customInc genericTls, T.name.len * 1
  43. customInc genericGlobal, T.name.len * 100
  44. customInc genericMixedPragmas, T.name.len * 10000
  45. return local + genericTls + genericGlobal + genericMixedPragmas
  46. echo nonGenericProc()
  47. echo nonGenericProc()
  48. echo genericProc(int)
  49. echo genericProc(int)
  50. echo genericProc(string)
  51. echo genericProc(string)
  52. proc echoInThread[T]() {.thread.} =
  53. echo genericProc(T)
  54. echo genericProc(T)
  55. proc newEchoThread(T: typedesc) =
  56. var t: Thread[void]
  57. createThread(t, echoInThread[T])
  58. joinThreads(t)
  59. newEchoThread int
  60. newEchoThread int
  61. newEchoThread float