Computation with time constraint

As far as system threads are concerned, the semantics of signals tries to mimic POSIX. The thread that receives the signal is chosen among the ones that do not mask the signal (see Thread.sigmask). Not terribly useful to cancel several threads on different timeouts.

A certain other language allows the cancellation of a chosen thread from any other thread by causing an an exception to arise at a distance. This would allow to implement timeouts for any number of threads from a controlling thread. Cancellation is not implemented with systhreads, although it does not seem to cause any more difficulty than the kind of asynchronous exceptions discussed in this topic.

Another option is if we had a way of executing a callback at regular intervals inside each thread and run a check there (and be able to raise an exception if we wished to interrupt the thread). This is essentially how memprof-limits works, which I wrote to show that with the new Memprof feature it is possible to implement allocation limits, a feature from a certain other language that addresses a use-case similar to yours (but which counts allocations as a measure of work done, rather than time elapsed). This is still experimental but I would like to publish a usable version soon.

In the absence of thread cancellation, this could be used to check for time elapsed or any other condition, but you need the additonal assumption that the threads allocate enough to trigger memprof callbacks (in contrast with the problem of memory limits which is similar to memory profiling).

1 Like