Created
December 30, 2017 17:12
-
-
Save krishnakumar4a4/6eb1e729a9d7bb2a70c6b6590d5ead56 to your computer and use it in GitHub Desktop.
Extending thread implementation to get unique identifier directly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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