tthread_generic.nim 953 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. discard """
  2. cmd: "nim $target --hints:on --threads:on $options $file"
  3. action: compile
  4. """
  5. type
  6. ThreadFuncArgs[T] = object of RootObj
  7. a: proc(): T {.thread.}
  8. b: proc(val: T) {.thread.}
  9. proc handleThreadFunc(arg: ThreadFuncArgs[int]){.thread.} =
  10. var fn = arg.a
  11. var callback = arg.b
  12. var output = fn()
  13. callback(output)
  14. proc `@||->`*[T](fn: proc(): T {.thread.},
  15. callback: proc(val: T){.thread.}): Thread[ThreadFuncArgs[T]] =
  16. var thr: Thread[ThreadFuncArgs[T]]
  17. var args: ThreadFuncArgs[T]
  18. args.a = fn
  19. args.b = callback
  20. createThread(thr, handleThreadFunc, args)
  21. return thr
  22. proc `||->`*[T](fn: proc(): T{.thread.}, callback: proc(val: T){.thread.}) =
  23. discard fn @||-> callback
  24. when true:
  25. import os
  26. proc testFunc(): int {.thread.} =
  27. return 1
  28. proc callbackFunc(val: int) {.thread.} =
  29. echo($(val))
  30. var thr = (testFunc @||-> callbackFunc)
  31. echo("test")
  32. joinThread(thr)
  33. os.sleep(3000)