No description
Find a file
Roger Shi 74ff85bb9d
Merge pull request #2 from brjohnsn/develop
Update code to Nim v0.17.0 specs
2017-11-13 17:51:06 +08:00
example Fix exising build and runtime errors 2017-05-30 06:53:43 -04:00
src/nimroutine Update code to eliminate deprecation warnings 2017-05-31 08:00:34 -04:00
.gitignore init commit 2015-09-15 17:05:50 +08:00
LICENSE.md Create LICENSE.md 2015-09-28 14:07:09 +08:00
nimroutine.nimble Update version to v0.1.2 2015-10-23 10:57:57 +08:00
README.md Update README.md 2015-09-30 21:46:43 +08:00

nim-routine

A go routine like nim implementation

Features

  • Light-weight task.
  • Go channel like message box (communication between tasks).
  • Support recursive task creating, that is to create new task in task itself.

Routine (Task)

  • How to define a routine?

It looks like normal proc, adding routine proc. For example:

proc foo(x: int) {.routine.} =
  echo "routine foo: ", x
  • How to run routine?

pRun is the proc run routine, followed by routine name and a tuple. The tuple contains the routine's parameters. For example:

pRun foo, (x: 1)
  • What about generic type?
proc foo[T](x: T) {.routine.} =
  echo "routine foo: ", x
pRun foo[int], (x: 1)
  • If the parameter is void?
proc foo() {.routine.} =
  echo "routine void param"
pRun foo
  • How to wait a task?
var watcher = pRun(foo)
wait(watcher)
  • How to wait all tasks?
waitAllRoutine()

MsgBox (Channel)

There're two kinds of MsgBox: sync and async. For sync msgbox, sender wait until there's a receiver get the message. For async msgbox, send continues running withoug waiting except msgbox is full (holding message count == capacity). Msgbox's capacity is determined while creating.

  • Create sync MsgBox
var msgBox = createSyncMsgBox[int]()
  • Create async MsgBox
var msgBox = createMsgBox[int]()  # msgBox's capacity is -1, which means unlimited.
var sizedMsgBox = createMsgBox[int](10) # msgBox that can hold 10 message
  • Send message
send(msgBox, 1) # 1 is the data to be sent
  • Receive mssage
var data: int
recv(msgBox, data) # data is assigned to msg value

Limitation

  • Not support var param in task. Walkaround: ptr.
  • No return value is support