YAP 7.1.0
Thread Synchronisation

All internal Prolog operations are thread-safe. More...

Detailed Description

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 MUTual EXclusive device, which implies 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

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).
asserta(+ C)
retractall(+ G)
mutex_create(? MutexId)

Class Documentation

◆ mutex_create/1

class mutex_create/1

mutex_create(? MutexId)

Create a mutex if MutexId is an atom, a named mutex is created If it is a variable, an anonymous mutex reference is returned There is no limit to the number of mutexes that can be created

◆ current_mutex/3

class current_mutex/3

current_mutex(? MutexId, ? ThreadId, ? Count)

Enumerates all existing mutexes If the mutex is held by some thread, ThreadId is unified with the identifier of the holding thread and Count with the recursive count of the mutex Otherwise, ThreadId is [] and Count is 0