pre-RFC, JoinHandle::try_join()

I came across this while writing some tests and believe that it is also useful for software engineering outside of tests.

Currently, when a new thread is spawned, it returns a JoinHandle with which you can get the thread handle or join (wait for it to run to completion). What I am suggesting is the ability to test if a thread has finished or not.

It would have the following signature: pub fn try_join(self) -> Option<Result<T>>. I think that this would be useful for work sharing or even monitoring since you can get some information about the thread without blocking yours. (kill if it is running to long but still do work in the meantime).

Can that be implemented portably? On Linux with glibc, I know of pthread_tryjoin_np, but that np indicates that this is non-Posix. I don’t know about the other unix-ish targets, let alone Windows and others.

1 Like

I found https://crates.io/crates/thread_tryjoin

It does use pthread_tryjoin_np, so this only works on Linux, but both linux-gnu and linux-musl should be fine. That crate is not trying to decode it into an Option as you suggest, so you’d have to interpret the EBUSY error yourself.

try_join would be trivial on Windows. Just change the timeout to 0, or even some number if you want a join_timeout.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.