thread_peek_message/2

thread_peek_message(+ Queue, ? Term)*

As thread_peek_message//11, operating on a given queue. It is allowed to peek into another thread's message queue, an operation that can be used to check whether a thread has swallowed a message sent to it.

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. Below is a simple implementation where the workers execute arbitrary Prolog goals. Note that this example provides no means to tell when all work is done. This must be realised using additional synchronisation.


 % create_workers(+Id, +N)
 %
 % Create a pool with given Id and number of workers.

 create_workers(Id, N) :-
 message_queue_create(Id),
 forall(between(1, N, _),
 thread_create(do_work(Id), _, [])).

 do_work(Id) :-
 repeat,
 thread_get_message(Id, Goal),
 ( catch (Goal, E, print_message(error, E))
 -> true
 ; print_message(error, goal_failed(Goal, worker(Id)))
 ),
 fail.

 % work(+Id, +Goal)
 %
 % Post work to be done by the pool

 work(Id, Goal) :-
 thread_send_message(Id, Goal).