Przeglądaj źródła

Merge branch 'hw8' of github.com:themultiplexer/bsys-ws17-grp11 into hw8

Lorenz Bung 7 lat temu
rodzic
commit
97c6ff145e
2 zmienionych plików z 27 dodań i 7 usunięć
  1. 3
    1
      hw8/task1/src/cli.yml
  2. 24
    6
      hw8/task1/src/main.rs

+ 3
- 1
hw8/task1/src/cli.yml Wyświetl plik

@@ -1,4 +1,6 @@
1
-name: "task1"
1
+name: "Proof of Work Mechanism 0.2"
2
+author: "Lorenz Bung & Joshua Rutschmann"
3
+about: "now the basics in Rust, no C anymore"
2 4
 args:
3 5
     - base:
4 6
         value_name: "base"

+ 24
- 6
hw8/task1/src/main.rs Wyświetl plik

@@ -19,16 +19,18 @@ pub fn main() {
19 19
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
20 20
     let threads = matches.value_of("threads").unwrap_or(&cpus);
21 21
     let sync = matches.is_present("sync");
22
+    let wait = matches.is_present("wait");
22 23
     let verbosity = matches.occurrences_of("verbose"); 
23 24
     
24 25
     let mut time_measurement = false;
25
-    let mut found = false;
26 26
 
27
+    // Falls irgendein Zeichen nicht hexadezimal ist, breche ab.
27 28
     if diff.chars().any(|c| !c.is_digit(16)) {
28 29
         println!("Difficulty is not hexadecimal.");
29 30
         process::exit(1)
30 31
     }
31 32
 
33
+    // Falls das Unterkommando timings angegeben wurde aktiviere die Zeitmessung.
32 34
     if let Some(ref sub_command) = matches.subcommand {
33 35
         if sub_command.name.eq("timings") {
34 36
             time_measurement = true;
@@ -59,26 +61,34 @@ pub fn main() {
59 61
             let mut children = vec![];
60 62
 
61 63
             let (tx, rx) = channel();
64
+            let mut found = &false;
62 65
 
63 66
             for i in 0..t {
64 67
                 let d = diff.clone();
65 68
                 let tx = tx.clone();
66
-
69
+                
67 70
                 children.push(thread::spawn(move || {
68 71
                     let mut n = i;
69 72
                     while n < max {
70 73
 
71
-                        if found && sync {
72
-                            process::exit(0);
74
+                        if n % (100000 + i)  == 0 {
75
+                            println!("Thread {}: {}", i, n);
76
+                        }
77
+
78
+                        if *found && sync {
79
+                            return n; 
73 80
                         }
74 81
 
75 82
                         if let Some(solution) = verify_product(b, n, &d) {
76
-                            found = sync;
83
+                            *found = true;
77 84
                             tx.send(solution).unwrap();
85
+                            if sync {
86
+                                break;
87
+                            }
78 88
                         }
79 89
                         n += t;
80 90
                     }
81
-
91
+                    n
82 92
                 }));
83 93
             }
84 94
 
@@ -96,6 +106,14 @@ pub fn main() {
96 106
                 }
97 107
                 Err(_) => {}
98 108
             }
109
+
110
+            if wait {
111
+                for child in children {
112
+                    let loops = child.join();
113
+                    println!("Loops: {}", loops.unwrap());
114
+                }
115
+            }
116
+        
99 117
         }
100 118
         (_, Err(_)) => {
101 119
             println!("Number of threads is not integer.");

Ładowanie…
Anuluj
Zapisz