The API will be based on the existing SWI-Prolog’s Thread communication API, that uses message queues. This will allow communication between any two threads.
Local Communication (Two threads that reside on the same processing node)
Prolog threads can exchange data using dynamic predicates, database records, and other globally shared data. These provide no suitable means to wait for data or a condition as they can only be checked in an expensive polling loop. Message queues provide a means for threads to wait for data or conditions without using the CPU.
Each thread has a message-queue attached to it that is identified by the thread. Additional queues are created using message_queue_create/1.
Remote Communication (Two threads that reside on different processing nodes)
If one thread reside on node X and the second in node Y then:
- send msg to node Y via Madeleine.
- thread listener on node Y will proxy message to thread message queue.
I’ll be mainly using SYS V IPC, more precisely message queues and eventually semaphores.
Explicit message queues are designed with the worker-pool model in mind, where multiple threads wait on a single queue and pick up the first goal to execute. I don’t know if the user will need to protect the queue against concurrent access (two or more threads reading from the queue at the same time) or if the SYS V message queues will take care of that.
If it will involve protecting the queues, then the handling of user-level mutexes or semaphores might get handy. A mutex is a MUTual EXclusive device and the difference between a mutex and a semaphore (as my good friend Pedro told me) is that a mutex is only for mutual exclusion and semaphores can be used for more complicated stuff. For example, with a mutex protecting a resource, only one process at a time can access it. With semaphores (depending on the implementation, of course), you can have two, or three, or as much processes as you like accessing the resource at a time.