Since var T parameters are not currently supported in spawned tasks, you can pass the address of a variable declared outside the awaitAll block using addr. Because this involves shared mutable state, you must protect the access with a lock. malebolgia/ticketlocks provides a TicketLock which is convenient as it does not require manual deinitLock calls.
import std / [strutils, tables]
import malebolgia
import malebolgia / ticketlocks
proc countWords(filename: string; results: ptr CountTable[string]; L: ptr TicketLock) =
for w in splitWhitespace(readFile(filename)):
withLock L[]:
results[].inc w
proc main() =
var m = createMaster()
var results = initCountTable[string]()
var L = initTicketLock()
m.awaitAll:
m.spawn countWords("temp.nim", addr results, addr L)
m.spawn countWords("tester.nim", addr results, addr L)
results.sort()
echo results