[m-users.] Calling exit(3) from a foreign predicate

Mark Brown mark at mercurylang.org
Fri Jan 26 06:30:23 AEDT 2024


On Fri, Jan 26, 2024 at 12:31 AM Volker Wysk <post at volker-wysk.de> wrote:
>
> Am Donnerstag, dem 25.01.2024 um 13:31 +1100 schrieb Mark Brown:
> > On Thu, Jan 25, 2024 at 1:40 AM Volker Wysk <post at volker-wysk.de> wrote:
> > > Calling exit(3), without calling mercury_runtime_terminate() before, works.
> > > All thread are terminated and the program exits. It's only
> > > mercury_runtime_terminate() that blocks. I've read somewhere that it waits
> > > for all threads to terminate. Is there another function which doesn't? I'd
> > > like to properly shut down the Mercury runtime, if possible.
> >
> > How was it started in the first place? If you entered via main/2 then
> > in principle the "proper" way to leave would be by exiting main/2. If
> > you used the standalone interface then mercury_terminate() is
> > documented right after mercury_init() in the User's Guide.
>
> My master thread (the main program) spawns some worker threads, which begin
> in the workers' main predicate. Which they leave after they have received an
> "end" message (from a channel). The workers don't get started in main/2.

Okay, though I was asking how the Mercury runtime started, not how
your threads started.

>
> And I'm not using the standalone interface.
>
> It's clear to me, that the threads should terminate by leaving the
> predicate, which they have started in, as specified for the thread.spawn/3
> predicate.

Right, shutting down the Mercury runtime is not actually the problem,
only stopping the threads beforehand is. For now having all the
threads poll an mvar seems to be the simplest solution, though you'd
have to figure out how to make the workers check it frequently enough.

So I can see the reason for wanting to asynchronously stop all threads
after leaving main/2. If Mercury was going to do that, an interface
similar to io.set_exit_status/3 would probably be preferred over
calling an exit predicate. That is, you'd set a flag to say what
happens to threads after leaving main/2. I don't know what
implementing this would involve, though.

Cheers,
Mark


More information about the users mailing list