Multithreading

Post Reply
User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Multithreading

Post 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

Post Reply