Last active
March 1, 2021 00:43
-
-
Save michaelsbradleyjr/005ae0751062532d97aa40e125160277 to your computer and use it in GitHub Desktop.
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
import os, threadpool | |
type | |
ThreadSafeString* = distinct cstring | |
proc safe*(input: string): ThreadSafeString = | |
var res = cast[cstring](allocShared(input.len + 1)) | |
copyMem(res, input.cstring, input.len) | |
res[input.len] = '\0' | |
res.ThreadSafeString | |
proc `$`*(input: ThreadSafeString): string = | |
result = $(input.cstring) | |
deallocShared input.cstring | |
proc sendSignal(data: ThreadSafeString, name: ThreadSafeString) = | |
echo $name & ": " & $data | |
template spawnAndSend(name: ThreadSafeString, exprToSend: untyped) = | |
proc payload() = | |
let data: ThreadSafeString = exprToSend | |
sendSignal(data, name) | |
spawn payload() | |
proc foo(name: ThreadSafeString) = | |
spawnAndSend(name) do: | |
"foo".safe | |
foo("bar".safe) | |
sleep(2) | |
# Run with `./env.sh nim c -r --threads:on --tlsEmulation:off bar.nim` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment