Struct nri::comms::RestartableThread [-] [+] [src]

pub struct RestartableThread<Data: Send + 'static> {
    tx: Option<Sender<Data>>,
    thread: Option<JoinHandle<()>>,
}

Container for a thread that repeatedly performs some action in response to input. Can be stopped and restarted.

Fields

tx

Sending end of a channel used to send inputs to the thread. Wrapped in a Option so it can be dropped without moving self.

thread

Handle to the running thread Wrapped in an option so it can be joined without moving self.

Methods

impl<Data: Send + 'static> RestartableThread<Data>

fn new<F>(f: F) -> RestartableThread<Data> where F: Send + 'static + Fn(Data)

Create a new RestartableThread which performs the given action in response to input. The thread will run (and wait for input) until RestartableThread::join() is called or the RestartableThread instance is dropped. To pass input, use RestartableThread::send().

fn join(&mut self)

Kill the thread. This shuts down the message queue, causing the thread to exit, and then waits for it to finish up any outstanding work. No deadlocks here!

fn send(&self, d: Data) -> Result<(), SendError<Data>>

Send some input to the thread. Nonblocking. Returns a SendError if Sender::send() fails or if the private Sender has somehow disappeared (which is impossible).

Trait Implementations

impl<Data: Send + 'static> Drop for RestartableThread<Data>

fn drop(&mut self)

When the RestartableThread goes out of scope, kill the thread.