tthreadvars.nim 1.5 KB

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