Default threading model of a Prolog program

I’ve encountered a few predicates that give warnings about threading. For example: unload_file/1 says:

“…attempts to do this should be reserved for development or situations where the application can guarantee that none of the clauses associated to File are active.”

and the library(persistency) docs say:

“This module requires the same thread-synchronization as the normal Prolog database. This implies that if each individual assert or retract takes the database from one consistent state to the next, no additional locking is required. If more than one elementary database operation is required to get from one consistent state to the next, both updating and querying the database must be locked using with_mutex/2.”

My assumption is that, unless I use a predicate that explicitly creates a thread, all of my code is single threaded, at least from the perspective of the library calls. I.e. the underlying system may use threads but that is hidden from me and I don’t have to worry about locking.

Is this correct?

I think this is no longer true :slight_smile:

Yes. By default SWI-Prolog uses two threads: main and gc. The first is your application, the second does atom and clause garbage collection. If you run the development tools such as gtrace/0 or PceEmacs, a pce thread is added that does the GUI so you can edit, examine resource usage, etc. while your application is running.

Using swipl --no-threads it runs completely single threaded. There are very few use cases for that. You can also build a single threaded version from the sources. That makes the core a little smaller and marginally faster.

1 Like