summary refs log tree commit diff stats
path: root/tests/accept/compile/tthread_generic.nim
blob: beae4b6527d355d5032cad25d3a3671729feebd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
discard """
  cmd: "nimrod cc --hints:on --threads:on $# $#"
"""

type
  TThreadFuncArgs[T] = object of TObject
    a: proc(): T {.thread.}
    b: proc(val: T) {.thread.}

proc handleThreadFunc(arg: TThreadFuncArgs[int]){.thread.} =
  var func = arg.a
  var callback = arg.b
  var output = func()
  callback(output)

proc `@||->`*[T](func: proc(): T {.thread.}, 
                 callback: proc(val: T){.thread.}): TThread[TThreadFuncArgs[T]] =
  var thr: TThread[TThreadFuncArgs[T]]
  var args: TThreadFuncArgs[T]
  args.a = func
  args.b = callback
  createThread(thr, handleThreadFunc, args)
  return thr

proc `||->`*[T](func: proc(): T{.thread.}, callback: proc(val: T){.thread.}) =
  discard func @||-> callback

when isMainModule:
  import os
  proc testFunc(): int {.thread.} =
    return 1
  proc callbackFunc(val: int) {.thread.} =
    echo($(val))
   
  var thr = (testFunc @||-> callbackFunc)
  echo("test")
  joinThread(thr)
  os.sleep(3000)