Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Created March 17, 2015 19:45
Show Gist options
  • Save PhilipWitte/1ea2efa2e04096ba9005 to your computer and use it in GitHub Desktop.
Save PhilipWitte/1ea2efa2e04096ba9005 to your computer and use it in GitHub Desktop.
import math, threadPool
# ---
type
Person = object
age: int
friend: ref Person
var
people: seq[ref Person] = @[]
proc newPerson(age:int): ref Person =
result.new()
result.age = age
proc greet(p:Person) =
echo p.age, ", ", p.friend.age
p.friend.age += 1
#echo "Person {",
# " age:", p.age, "(", cast[int](addr p.age),"),",
# " friend:", p.friend.age, "(", cast[int](addr p.friend.age),") }"
# ---
proc setup =
for i in 0 .. <20:
people.add newPerson(i + 1)
for i in 0 .. <20:
people[i].friend = people[random(20)]
proc update =
var countA: array[20, int]
var countB: array[20, int]
for i, p in people:
countA[i] = getRefCount(p)
parallel:
for i in 0 .. people.high:
spawn people[i].greet()
for i, p in people:
countB[i] = getRefCount(p)
for i in 0 .. <20:
echo countA[i], ", ", countB[i]
if countA[i] != countB[i]:
echo "NOT THE SAME!"
# ---
when isMainModule:
randomize()
setup()
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment