diff --git a/examples/subscriber_with_thread.rs b/examples/subscriber_with_thread.rs index 7375d11..422a75a 100644 --- a/examples/subscriber_with_thread.rs +++ b/examples/subscriber_with_thread.rs @@ -4,6 +4,7 @@ use std::thread; use r2r; use r2r::std_msgs; +// This example listens to /topic and publishes to /hello and /count fn main() -> Result<(), Box> { let ctx = r2r::Context::create()?; let mut node = r2r::Node::create(ctx, "testnode", "")?; @@ -37,14 +38,25 @@ fn main() -> Result<(), Box> { pubint.publish(&to_send).unwrap(); }; - let _ws2 = node.subscribe("/hi", Box::new(cb))?; + let _ws2 = node.subscribe("/topic", Box::new(cb))?; - // run for 10 seconds + // here we spin the node in its own thread + let handle = std::thread::spawn(move || { + let mut count = 0; + while count < 100 { + node.spin_once(std::time::Duration::from_millis(100)); + count += 1; + } + }); + + // give the thread time to run let mut count = 0; - while count < 100 { - node.spin_once(std::time::Duration::from_millis(100)); + while count < 10 { + std::thread::sleep(std::time::Duration::from_millis(1000)); + println!("hello from main thread"); count += 1; } + handle.join().unwrap(); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index e91b77c..72928bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -635,6 +635,8 @@ pub struct Node { pubs: Vec>, } +unsafe impl Send for Node {} + impl Node { pub fn name(&self) -> Result { let cstr = unsafe { rcl_node_get_name(self.node_handle.as_ref()) };