docs.rs failed to build terminate-thread-0.3.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
What is this?
It's just a simple terminatable thread implement with pthread for rust
But Why?
Sometimes, I need to terminate a blocked thread. There is no way to
do it with the standard std::thread without putting into some Sync thing.
How to use it?
[]
= "0.3"
Spawn your thread
use Thread;
spawn.join; // ← spawn & join (>= 0.3.0) your thread
Manually terminate your thread
use Thread;
let thr = spawn;
sleep;
thr.terminate // ← the thread is terminated manually!
Auto terminate your thread
use Thread;
Panic tolerant (v0.3.1, macOS only)
use Thread;
spawn; // ← this is fine
let thread = spawn.join; // ← thread stores the panic info
assert!; // ← it's over and panics
let info = thread.panic_info.lock.unwrap.take.unwrap; // ← take out the panic info
assert_eq!; // ← get your panic info
Not a good idea!
Terminate a running thread is ALWAYS A BAD IDEA!
The better way is to use something like std::sync::atomic::AtomicBool,
to give your thread a chance to return.
Tested Platform
- Linux
- macOS
It should work in any platform support pthread,
but the real world is sophisticated to make any promise.
To-do
- Terminate the job which panics. >= v0.3.0
use Thread;
spawn; // ← this is fine
let thread = spawn.join; // ← thread stores the panic info
assert!; // ← it's over and panics
Issue
- Terminate the thread too quick panics. >= v0.2.0
use Thread;
spawn; // ← bus error
-
std::panic::AssertUnwindSafe()does not work in linux. == v0.3.0
use Thread;
spawn; // ← FATAL: exception not rethrown