-// ## Arrays
-fn sort_array() {
- let mut array_of_data: [f64; 5] = [1.0, 3.4, 12.7, -9.12, 0.1];
- sort(&mut array_of_data);
+// With the operations of the three threads defined, we can now implement a function that performs grepping according
+// to some given options.
+pub fn run(options: Options) {
+ // We move the `options` into an `Arc`, as that's what the thread workers expect.
+ let options = Arc::new(options);
+
+ // This sets up the channels. We use a `sync_channel` with buffer-size of 16 to avoid needlessly filling RAM.
+ let (line_sender, line_receiver) = sync_channel(16);
+ let (filtered_sender, filtered_receiver) = sync_channel(16);
+
+ // Spawn the read thread: `thread::spawn` takes a closure that is run in a new thread.
+ let options1 = options.clone();
+ let handle1 = thread::spawn(move || read_files(options1, line_sender));
+
+ // Same with the filter thread.
+ let options2 = options.clone();
+ let handle2 = thread::spawn(move || {
+ filter_lines(options2, line_receiver, filtered_sender)
+ });
+
+ // And the output thread.
+ let options3 = options.clone();
+ let handle3 = thread::spawn(move || output_lines(options3, filtered_receiver));
+
+ // Finally, wait until all three threads did their job.
+ handle1.join().unwrap();
+ handle2.join().unwrap();
+ handle3.join().unwrap();