Page 1 of 1

Multithreading

Posted: Mon Jun 26, 2017 2:17 pm
by Mr. Kibernetik
SPL can execute multiple threads at the same time, also it can access the same object from multiple threads.
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~
.
Output of the program:

Code: Select all

-> +
-> -
-> +
-> -
<- +
<- -
<- -
<- +
result = 0