Concurrency
Intro to Concurrency
Concurrency is a complex and error-prone field. The basis of concurrency is the concept of threads that execute code concurrently. The difficulties arise when these threads interact with one another by sharing data or waiting for a concurrent computation to finish.
Sharing data between threads bears the risk of inconsistent data that is seen by a thread while waiting may result in a deadlock (threads waiting for one another in a cycle) or live-locks when threads run forever waiting for some state change that never occurs.
Immutable data and Channels
The solution in Fuzion is using immutable data that is safe to be accessed by
multiple threads wherever possible. When data is to be shared between
threads, the base library provides safe mechanisms, most
notably Channels that permit the exchange of data between
concurrent threads.
Controlled Shared Memory Access
However, lower-level shared memory communication is also possible. Shared memory thread interactions in Fuzion cannot occur by accident, but they must be made explicit such that possible errors can be found as early as possible, at compile time or through runtime checks if a compile-time check is not possible.
Shared mutable data requires exclusive access by the accessing thread and this
exclusivity must be ensured by some locking mechanism. This is possible
for mutable data created using a thread-safe blocking_mutate
effect that enforces accesses to be exclusive by the current thread.
Atomic and Racy Accesses
Low-level hardware mechanisms like compare-and-set or read and write fences are error-prone and difficult to use. Nevertheless, Fuzion provides APIs to use these low-level mechanisms since there are performance-critical algorithms that require these.