Last active
August 14, 2017 15:26
-
-
Save Elzair/92e9964ff575f5ad42fef7868694d55c to your computer and use it in GitHub Desktop.
work-sharing-threadpool-help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiling playground v0.0.1 (file:///playground) | |
error[E0308]: mismatched types | |
--> src/main.rs:62:23 | |
| | |
62 | mem::swap(datum1.unwrap().1.borrow_mut(), | |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &mut _, found struct `std::cell::RefMut` | |
| | |
= note: expected type `&mut _` | |
found type `std::cell::RefMut<'_, std::sync::mpsc::Receiver<bool>>` | |
= help: try with `&mut datum1.unwrap().1.borrow_mut()` | |
error[E0308]: mismatched types | |
--> src/main.rs:63:23 | |
| | |
63 | datum2.unwrap().1.borrow_mut()); | |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &mut _, found struct `std::cell::RefMut` | |
| | |
= note: expected type `&mut _` | |
found type `std::cell::RefMut<'_, std::sync::mpsc::Receiver<bool>>` | |
= help: try with `&mut datum2.unwrap().1.borrow_mut()` | |
error: aborting due to previous error(s) | |
error: Could not compile `playground`. | |
To learn more, run the command again with --verbose. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::RefCell; | |
use std::mem; | |
use std::sync::mpsc; | |
use std::sync::mpsc::{Receiver,Sender}; | |
trait FnBox { | |
fn call_box(self: Box<Self>); | |
} | |
impl<F: FnOnce()> FnBox for F { | |
fn call_box(self: Box<F>) { | |
(*self)(); | |
} | |
} | |
pub type Task = Box<FnBox + Send + 'static>; | |
struct ThreadPool { | |
// workers: Vec<Worker>, | |
} | |
impl ThreadPool { | |
pub fn new(num_threads: usize) -> Result<ThreadPool, ()> { | |
let is_same_channel = |n: usize, row_size: usize| -> bool { | |
(n / row_size) == (n % row_size) | |
}; | |
//let is_lower_left = |y: usize, x: usize| -> bool { | |
// y > x | |
//}; | |
let get_n = |y: usize, x: usize, row_size: usize| { | |
y * row_size + x | |
}; | |
// Model the channels as an nxn matrix. | |
let data1 = | |
(0..(num_threads*num_threads)).map(|n| { | |
// Do not create a channel for the thread to communicate | |
// with itself. | |
match is_same_channel(n, num_threads) { | |
true => None, | |
false => { | |
let (rqst_tx, rqst_rx) = mpsc::channel::<bool>(); | |
let (resp_tx, resp_rx) = mpsc::channel::<bool>(); | |
let (jobs_tx, jobs_rx) = mpsc::channel::<Task>(); | |
Some((rqst_tx, RefCell::new(rqst_rx), | |
RefCell::new(resp_tx), resp_rx, | |
jobs_tx, RefCell::new(jobs_rx))) | |
} | |
} | |
}).collect::<Vec<_>>(); | |
let coords = | |
(0..(num_threads*num_threads)).map(|n| { | |
(n / num_threads, n % num_threads) | |
}).filter(|&(y, x)| { | |
y > x | |
}).collect::<Vec<_>>(); | |
for &(y, x) in coords.iter() { | |
let datum1 = &data1[get_n(y, x, num_threads)]; | |
let datum2 = &data1[get_n(x, y, num_threads)]; | |
mem::swap(datum1.unwrap().1.borrow_mut(), | |
datum2.unwrap().1.borrow_mut()); | |
} | |
Ok(ThreadPool{}) | |
} | |
} | |
fn main() { | |
ThreadPool::new(8); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment