All internal Prolog operations are thread-safe. This implies that two Prolog threads can operate on the same dynamic predicate without corrupting the consistency of the predicate. This section deals with user-level mutexes (called monitors in ADA or critical sections by Microsoft). A mutex is a MUTual EXclusive device, which implies that at most one thread can hold a mutex.
Mutexes are used to realise related updates to the Prolog database. With‘related', we refer to the situation where a‘transaction' implies two or more changes to the Prolog database. For example, we have a predicate address/2 , representing the address of a person and we want to change the address by retracting the old and asserting the new address. Between these two operations the database is invalid: this person has either no address or two addresses, depending on the assert/retract order.
The code below provides a solution to this problem based on with_mutex/2. It also illustrates the problem of mutexes. The predicate with_mutex/2 behaves as once/1 with respect to the guarded goal. This means that our predicate address/2 is no longer a nice logical non-deterministic relation. This could be solved by explicit locking and unlocking a mutex using setup_call_cleanup/3, but at the risk of deadlocking the program if the choice point is left open by accident.
change_address(Id, Address) :- with_mutex(addressbook, ( retractall(address_db(Id, _)), asserta(address_db(Id, Address)) )). address(Id, Address) :- with_mutex(addressbook, address_db(Id, Address)).
Message queues (see message_queue_create/2) often provide simpler and more robust ways for threads to communicate. Still, mutexes can be a sensible solution and are therefore provided.
mutex_create(X, [alias(name)])
is
preferred over the equivalent mutex_create(name)
.existence_error
exception. As of version
7.1.19, this behaviour is reliable. If the mutex is locked, the mutex is
scheduled for delayed destruction: it will be destroyed when it
becomes unlocked.Although described in the thread section, this predicate is also available in the single-threaded version, where it behaves simply as once/1.
If MutexId is an atom, and there is no current mutex with that name, the mutex is created automatically using mutex_create/1. This implies named mutexes need not be declared explicitly.
Please note that locking and unlocking mutexes should be paired carefully. Especially make sure to unlock mutexes even if the protected code fails or raises an exception. For most common cases, use with_mutex/2, which provides a safer way for handling Prolog-level mutexes. The predicate setup_call_cleanup/3 is another way to guarantee that the mutex is unlocked while retaining non-determinism.
permission_error
exception is raised.unlocked
if the mutex
is currently not locked, or locked(Owner, Count)
if mutex
is locked
Count times by thread Owner. Note that unless Owner
is the calling thread, the locked status can change at any time. There
is no useful application of this property, except for diagnostic
purposes.bugAs Owner
and Count are fetched separately from the mutex, the values
may be inconsistent.