ADT
        ast-experimental
        pthread-emulation
        qualifiedEnum
      
      
        
          | 
            Last change
 on this file since 6a6544d was             716b62c, checked in by Thierry Delisle <tdelisle@…>, 4 years ago           | 
        
        
          | 
             
Added rust and cpu example. 
 
           | 
        
        
          
            
              - 
Property                 mode
 set to                 
100644
               
             
           | 
        
        
          | 
            File size:
            1.4 KB
           | 
        
      
      
| Line |   | 
|---|
| 1 | use std::thread;
 | 
|---|
| 2 | use std::time::Duration;
 | 
|---|
| 3 | 
 | 
|---|
| 4 | // This is the version I wished worked.
 | 
|---|
| 5 | // If join was't ingored it would be safe
 | 
|---|
| 6 | fn better() {
 | 
|---|
| 7 |         // let durr = Duration::from_millis(1);
 | 
|---|
| 8 |         {
 | 
|---|
| 9 |                 let thrd = thread::spawn(move || {
 | 
|---|
| 10 |                         for i in 1..10 {
 | 
|---|
| 11 |                                 println!("hi number {} from the spawned thread!", i);
 | 
|---|
| 12 |                                 thread::sleep(durr);
 | 
|---|
| 13 |                         }
 | 
|---|
| 14 |                 });
 | 
|---|
| 15 | 
 | 
|---|
| 16 |                 for i in 1..5 {
 | 
|---|
| 17 |                         println!("hi number {} from the main thread!", i);
 | 
|---|
| 18 |                         thread::sleep(durr);
 | 
|---|
| 19 |                 }
 | 
|---|
| 20 | 
 | 
|---|
| 21 |                 thrd.join().unwrap();
 | 
|---|
| 22 |         }
 | 
|---|
| 23 | }
 | 
|---|
| 24 | 
 | 
|---|
| 25 | use std::sync::Arc;
 | 
|---|
| 26 | 
 | 
|---|
| 27 | // This uses arc, which should not be needed
 | 
|---|
| 28 | // But it fails to figure out where to clone so it doesn't compile
 | 
|---|
| 29 | fn best() {
 | 
|---|
| 30 |         let durr = Arc::new(Duration::from_millis(1));
 | 
|---|
| 31 |         {
 | 
|---|
| 32 |                 let thrd = thread::spawn(|| {
 | 
|---|
| 33 |                         for i in 1..10 {
 | 
|---|
| 34 |                                 println!("hi number {} from the spawned thread!", i);
 | 
|---|
| 35 |                                 thread::sleep(*durr);
 | 
|---|
| 36 |                         }
 | 
|---|
| 37 |                 });
 | 
|---|
| 38 | 
 | 
|---|
| 39 |                 for i in 1..5 {
 | 
|---|
| 40 |                         println!("hi number {} from the main thread!", i);
 | 
|---|
| 41 |                         thread::sleep(*durr);
 | 
|---|
| 42 |                 }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |                 thrd.join().unwrap();
 | 
|---|
| 45 |         }
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | // This is what is actually required
 | 
|---|
| 49 | // Note that the clone is explicit
 | 
|---|
| 50 | fn real() {
 | 
|---|
| 51 |         let durr = Arc::new(Duration::from_millis(1));
 | 
|---|
| 52 |         {
 | 
|---|
| 53 |                 let durr2 = durr.clone();
 | 
|---|
| 54 |                 let thrd = thread::spawn(move || {
 | 
|---|
| 55 |                         for i in 1..10 {
 | 
|---|
| 56 |                                 println!("hi number {} from the spawned thread!", i);
 | 
|---|
| 57 |                                 thread::sleep(*durr2);
 | 
|---|
| 58 |                         }
 | 
|---|
| 59 |                 });
 | 
|---|
| 60 | 
 | 
|---|
| 61 |                 for i in 1..5 {
 | 
|---|
| 62 |                         println!("hi number {} from the main thread!", i);
 | 
|---|
| 63 |                         thread::sleep(*durr);
 | 
|---|
| 64 |                 }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |                 thrd.join().unwrap();
 | 
|---|
| 67 |         }
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | fn main() {
 | 
|---|
| 71 |         best();
 | 
|---|
| 72 |         better();
 | 
|---|
| 73 |         real();
 | 
|---|
| 74 | }
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.