Libraries for IPC Queues?

Are there libraries for IPC Queues?

I want to make a program that finds primes and saves them at the same time. Thread 1 will find the primes. If Thread 1 finds a prime, then Thread 1 sends the computed prime to Thread 2 through a queue or some data structure. Thread 2 will serialize the primes and write them into a file.


fn is_prime(n: u64) -> bool {
    let limit = (n as f64).sqrt() as u64;

    for i in 2..=limit {
        if n % i == 0 {
            return false;
        }
    }

    true
}


fn main() {
    
    let mut queue;
    
    //Thread 1 Find Prime Numbers
    thread::spawn(|| {
      let mut number = 3;
      loop
      {
          if is_prime(number)
          {
              //Send to Thread 2
              queue.send(number);
          }
          number = number + 1;
      }
    });
    
     //Thread 2 Save Primes into files
      loop
      {
         if queue.len() > 0
         {
             for prime in queue
             {
                 let data = format!("{}",prime );
                 let filename = format!("{}.txt",prime );
                 fs::write(filename, data).expect("Unable to write file");
             }
         }
      }
}

There are thread-safe queues in the standard library:

A popular crate that provides richer functionality is Crossbeam:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.