Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,955 questions

51,897 answers

573 users

How to create a thread in Rust

2 Answers

0 votes
use std::thread;
use std::time::Duration;

fn main() {
   thread::spawn(|| {
      for i in 1..10 {
         println!("number {} - thread::spawn 1..10", i);
         thread::sleep(Duration::from_millis(1));
      }
   });
   for i in 1..5 {
      println!("number {} 1..5", i);
      thread::sleep(Duration::from_millis(2));
   }
}




/*
run:

number 1 1..5
number 1 - thread::spawn 1..10
number 2 - thread::spawn 1..10
number 2 1..5
number 3 - thread::spawn 1..10
number 4 - thread::spawn 1..10
number 3 1..5
number 5 - thread::spawn 1..10
number 6 - thread::spawn 1..10
number 4 1..5
number 7 - thread::spawn 1..10
number 8 - thread::spawn 1..10

*/

 



answered Nov 3, 2022 by avibootz
0 votes
use std::thread;
use std::time::Duration;

fn main() {
    let handle = thread::spawn(|| {
      for i in 1..10 {
         println!("number {} - thread::spawn 1..10", i);
         thread::sleep(Duration::from_millis(1));
      }
    });
    
    for i in 1..5 {
      println!("number {} 1..5", i);
      thread::sleep(Duration::from_millis(2));
    }
   
    handle.join().unwrap();
}




/*
run:

number 1 1..5
number 1 - thread::spawn 1..10
number 2 - thread::spawn 1..10
number 2 1..5
number 3 - thread::spawn 1..10
number 4 - thread::spawn 1..10
number 3 1..5
number 5 - thread::spawn 1..10
number 6 - thread::spawn 1..10
number 4 1..5
number 7 - thread::spawn 1..10
number 8 - thread::spawn 1..10
number 9 - thread::spawn 1..10

*/

 



answered Nov 3, 2022 by avibootz
...