Skip to content

Instantly share code, notes, and snippets.

@krishnakumar4a4
Created December 30, 2017 17:12
Show Gist options
  • Save krishnakumar4a4/6eb1e729a9d7bb2a70c6b6590d5ead56 to your computer and use it in GitHub Desktop.
Save krishnakumar4a4/6eb1e729a9d7bb2a70c6b6590d5ead56 to your computer and use it in GitHub Desktop.
Extending thread implementation to get unique identifier directly
use std::thread;
use std::thread::JoinHandle;
use std::thread::ThreadId;
fn main() {
let handle = thread::spawn(||{
println!("Spawned thread {:?}",thread::current().id());});
let handle2 = thread::spawn(||{
println!("Spawned thread2 {:?}",thread::current().id());
});
trait ThreadCount {
fn count(self) -> ThreadId;
}
impl<T> ThreadCount for JoinHandle<T> {
fn count(self) -> ThreadId {
self.thread().id()
}
}
//Here instead getting thread count by handle.thread().id(), we do it with simply handle2.count()
println!("handle {:?} {:?}",handle.thread().id(),handle2.count());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment