# pre-RFC, JoinHandle::try\_join()

**URL:** <https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110>\
**Category:** Uncategorized\
**Created:** [August 1, 2018, 4:52pm UTC](https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110 "2018-08-01T16:52:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [August 1, 2018, 4:52pm UTC](https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110/1 "2018-08-01T16:52:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [August 1, 2018, 5:30pm UTC](https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110/2 "2018-08-01T17:30:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [August 1, 2018, 6:14pm UTC](https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110/3 "2018-08-01T18:14:41Z")

</div>

I found [https://crates.io/crates/thread\_tryjoin](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.

---

<div class="post-metadata">

**Author:** ![retep998](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/retep998/32/729_2.png) [@retep998](https://internals.rust-lang.org/u/retep998)\
**Post date:** [August 15, 2018, 9:07pm UTC](https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110/4 "2018-08-15T21:07:56Z")

</div>

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

> <https://github.com/rust-lang/rust/blob/d679b57a3a234a75926f1ffc5fe088c7ad97daf0/src/libstd/sys/windows/thread.rs#L70>

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/pre-rfc-joinhandle-try-join/8110/5 "2019-03-25T08:30:38Z")

</div>

Closed on March 25, 2019, 8:30am UTC

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