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

Volker Wysk post at volker-wysk.de
Fri Jan 26 20:36:30 AEDT 2024


Am Freitag, dem 26.01.2024 um 06:30 +1100 schrieb Mark Brown:
> 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.

That's no problem, except for the hash function, which I make calculate a
murmurhash3 hash from a file. This is a very fast, non-cryptographic hash,
with a good "randomness"/distribution of the hashes. The implementation
which I have, doesn't do an incremental, chunk-wise calculation of the file.
The entire file needs to be in memory, and is hashed in one go. If it would
incrementally calculate the hash in chunks, one could check the global mvar
in between, but it doesn't. So I have to find another implementation, or
another hash, or hack the murmurhash3 function to make it work in chunks...

> 
> 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.

This sounds reasonable.


Cheers,
Volker


More information about the users mailing list