The predicate flag/3
is the oldest way to store global non-backtrackable data in SWI-Prolog.
Flags are global and shared by all threads. Their value is limited to
atoms, small (64-bit) integers and floating point numbers. Flags are
thread-safe. The flags described in this section must not be confused
with Prolog flags described in
section 2.12.
- get_flag(+Key,
-Value)
- True when Value is the value currently associated with Key.
If Key does not exist, a new flag with value‘0' (zero)
is created.
- set_flag(+Key,
Value)
- Set flag Key to Value. Value must be an atom,
small (64-bit) integer or float.
- flag(+Key, -Old,
+New)
- True when Old is the current value of the flag Key
and the flag has been set to New. New can be an
arithmetic expression. The update is atomic. This predicate can
be used to create a shared global counter as illustrated in the
example below.
next_id(Id) :-
flag(my_id, Id, Id+1).