Skip to content

Instantly share code, notes, and snippets.

@Yepoleb
Last active June 26, 2022 02:32
Show Gist options
  • Save Yepoleb/cab387724ec972b71faf614585856beb to your computer and use it in GitHub Desktop.
Save Yepoleb/cab387724ec972b71faf614585856beb to your computer and use it in GitHub Desktop.
# Compile: nim c --mm:arc --threads:on --threadanalysis:off threadbugs.nim
import threadpool
type
DataItem = object
value: int
ContainerItem = object
data: ref DataItem
proc destroy(x: ref DataItem) =
echo x.value, " destroyed"
proc newDataItem(val: int): ref DataItem =
new(result, destroy)
result.value = val
var items: seq[ref ContainerItem]
for i in 0..<1024:
var newItem = new ContainerItem
newItem.data = newDataItem(i)
items.add(newItem)
proc busyTask(search: int): int =
for cont in items:
let data = cont.data
if data.value == search:
return data.value
while true:
var tasks: seq[FlowVar[int]]
for i in 0..<1024:
tasks.add(spawn(busyTask(i)))
while tasks.len > 0:
let doneIndex = blockUntilAny(cast[seq[FlowVarBase]](tasks))
discard ^tasks[doneIndex]
tasks.del(doneIndex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment