Synchronization object


SyncObject

The synchronization object provides a kind of mutex and can be locked and unlocked to prevent critical sections of programs to be executed simultaneously. It also provides a kind of event (posix condition), threads can wait for to occur.

Synchronization basics

A mutex used for thread synchronization has 2 states: locked and unlocked. A mutex can never be owned by multiple threads simultaneously. If a thread tries to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex.

Events are another synchronization device. Threads can wait for an event or they can signal an event to wake up threads waiting for it. Events are implemented using posix events (see manpage 3 pthread_cond_init). Events are used together with mutexes to prevent race conditions. Before waiting for an event, the associated mutex must be locked. When the thread starts to wait for the event, the mutex is automatically unlocked. Before a wait returns the mutex is automatically locked again.

In Vimms all objects have an associated mutex that is locked before the object is executed. This ensures that no object is executed at the same time by more than one thread (except with some objects like the call and the thread-block object). This means if for example a var-bridge object is used by multiple threads to share data normally no additional mutex to prevent coinstantaneous access is required.

Using the synchronization object

The synchronization object has no properties but several actions. To lock and unlock the mutex use the actions lock and unlock. For waiting for an event use the wait action. Before calling the wait action the mutex has to be locked. For waking up threads that are waiting there exist two actions signal and broadcast. The signal action wakes up only one thread that is waiting for the event if there is at least one thread waiting. The broadcast action wakes up all threads that are waiting for the event.
Fig. 1: Two threads
Figure 1 shows an object layout where a numeric input objects sets some input data within two loops and afterwards wakes up the according threads.


The Vimms User Manual