[m-users.] What is the threading model

Paul Bone paul at bone.id.au
Fri Mar 7 14:46:32 AEDT 2014

On Fri, Mar 07, 2014 at 02:25:19AM +1300, ardeujho wrote:
> What is the threading model of Mercury? Does it depend on the grades? Is
> it native threads or some form of green threads?

It's grade dependent, basically:

low level C:    green threads running on a fixed number of OS threads.
high level C:   OS threads only
Java:           Java's threads
C#:             C#'s threads, I think I don't really know

In the C grades you must have the .par grade component to access these

The green threads in the low-level C grades cannot be preempted.  If one
runs an infinite loop it will not be interrupted to schedule another.

> If I write a foreign C procedure that blocks will it block other mercury
> threads that were created using 'spawn'? Or is this grade dependent?

Low level C:    Yes, but they might run on other OS threads anyway.
High level C:   No.

We'd like to make this more manageable in the future.  In particular even
built in IO operations can block other threads in the low-level C grades.

> Some simple testing seem to indicate I needed to call 'yield' to

To do what?

> and
> foreign calls block the system threads but I'm wondering if this is
> actually the case. If it is the case, what should I do to prevent
> blocking mercury threads?

Foreign calls without the 'thread_safe' annotation will block other foreign
calls without this annotation.  If your foreign code is thread safe then
adding this annotation will make the runtime system treat it as such.
However if the foreign code blocks there may still be problems.


Paul Bone

More information about the users mailing list