In this program four threads are launched: two are copies of PLUS function and are increasing object A by 1 ten million times, two are copies of MINUS function and are decreasing object A by 1 ten million times.
Main thread is awaiting while threads are execulting, and then prints the result: A remains 0.
Code: Select all
a = 0
fins = 0
n = 2
> i, 1..n
fplus[i] = plus
-> fplus[i]()
fminus[i] = minus
-> fminus[i]()
<
> fins!=n*2
<
#.output("result = ",a)
plus()=
#.output("-> +")
> i, 1..10000000
.a!
.a += 1
.a~
<
#.output("<- +")
.fins!
.fins += 1
.fins~
.
minus()=
#.output("-> -")
> i, 1..10000000
.a!
.a -= 1
.a~
<
#.output("<- -")
.fins!
.fins += 1
.fins~
.
Code: Select all
-> +
-> -
-> +
-> -
<- +
<- -
<- -
<- +
result = 0