In reply to muku_383:
There is no problem with the code shown above because each task finishes at a unique time. But you have race conditions if multiple tasks finish in the same region of a time step. Suppose you change task t4 to have a delay of #15. Then both t2 and t4 finish concurrently. It’s possible for
count to skip from 2 to 4 and
wait(count==3) never sees the expression being true.
Another problem is the ++ operator is a non-atomic read-modify-write operation that could be written as
count = count + 1;. This is always a problem when you have multiple threads accessing the same global variable.
The best way of handling this is using a semaphore. Each thread does a
sema.put(1), and replace the
wait with
sema.get(3);.