添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

In the example program below, if I wanted to be able to control an individual timer (eg pause ticker2, but leave ticker1 running) from outside of the thread, what would be the best way to do this in rust, I was thnking of just using a global variable but I figure there is a proper rust way to do it?

use crossbeam_channel::{select, tick};
use std::time::Duration;
use std::thread;
fn main() {
  start_timers();
  //code to pause/restart one of the timers???
fn start_timers() {
  let ticker1 = tick(Duration::from_secs(1));
  let ticker2 = tick(Duration::from_secs(5));
  thread::spawn(move || {
    loop {
      select! {
        recv(ticker1) -> _ => foo(),
        recv(ticker2) -> _ => foo2()
fn foo() {
  println!("bar");
fn foo2() {
  println!("bar2");
              

thanks alice, thats exactly the solution i arrived at!

Good to see I was on the right track!

Thanks!