Created
July 28, 2011 01:41
-
-
Save AzureKitsune/1110749 to your computer and use it in GitHub Desktop.
Nimrod thread enhancements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# Nimrod's Runtime Library | |
# (c) Copyright 2011 Alex Mitchell | |
# | |
# See the file "copying.txt", included in this | |
# distribution, for details about the copyright. | |
# | |
## :Author: Alex Mitchell | |
## | |
## This module implements a couple of operators for executing functions on a thread. An example of this is: | |
## .. code-block:: Nimrod | |
## proc generateNum(): int {.thread.} = | |
## return 1 | |
## proc handleNum(val: int) {.thread.} = | |
## echo($(val)) | |
## | |
## generateNum ||-> handeNum | |
## echo("This will print out first because generateNum won't block execution.") | |
## | |
## If you want a little more control, you could use the '@||->' operator instead which returns a TThread object. | |
## .. code-block:: Nimrod | |
## proc generateNum(): int {.thread.} = | |
## return 1 | |
## proc handleNum(val: int) {.thread.} = | |
## echo($(val)) | |
## | |
## var thread = (generateNum @||-> handeNum) | |
## joinThread(thread) #Wait for the thread to finish | |
## echo("This will print out second because we waited for the thread to finish.") | |
type | |
TThreadFuncArgs[T] = object of TObject | |
a: proc(): T {.thread.} | |
b: proc(val: T) {.thread.} | |
#proc `||->`* [S, T] (func: proc(params: S): T, callback: proc(val: T)) = | |
#var lock: TLock | |
#InitLock(lock) | |
#var thr: TThread | |
#createThread(thr, handleThreadFunc,func,lock,callback) | |
proc `@||->`* [T] (func: proc(): T {.thread.}, callback: proc(val: T) {.thread.}): TThread[TThreadFuncArgs[int]] = | |
## An Operator that calls a function asynchronously (using threads, allowing execution to continue), returns the thread, and feeds the output to a callback function. | |
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.}) = | |
## An Operator that calls a function asynchronously (using threads, allowing execution to continue) and feeds the output to a callback function. | |
discard func @||-> callback | |
proc handleThreadFunc (arg: TThreadFuncArgs[int]){.thread.} = | |
var func = arg.a | |
var callback = arg.b | |
var output: int | |
output = func() | |
callback(output) | |
when isMainModule: | |
import os | |
proc testFunc(): int {.thread.} = | |
return 1 | |
proc callbackFunc(val: int) {.thread.} = | |
echo($(val)) | |
#var thr = (testFunc @||-> callbackFunc) | |
# Operator returns a thread object so you can wait for it to finish if needed. | |
# This test sort of returns it because the program will exit before the thread completes | |
testFunc ||-> callbackFunc | |
echo("test") | |
#joinThread(thr) | |
os.sleep(3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment