Thread Synchronisation

All internal Prolog operations are thread-safe.

This implies 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 MUT__ual __EX__clusive device, which implies at most one thread can __hold a mutex.

Mutexes are used to realise related updates to the Prolog database. Withrelated, we refer to the situation where a transaction implies two or more changes to the Prolog database. For example, we have a predicate address//22, 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.

Here is how to realise a correct update:


 :- initialization
 mutex_create(addressbook).

 change_address(Id, Address) :-
 mutex_lock(addressbook),
 retractall(address(Id, _)),
 asserta(address(Id, Address)),
 mutex_unlock(addressbook).