Hello,
the function run converts a map into a queue. Threads get the items from this queue and consume it.
Is it possible to execute the function run without cloning the map in the function(let map=map.clone();)?
const MAX_THREADS:u8=8;
fn run(map:&HashMap<String, f64>)
{
let q = Arc::new(ArrayQueue::new(map.len()));
let map=map.clone();
let mut handles=Vec::with_capacity(map.len());
for item in map
{
q.push(item).expect("pushing failed");
}
for _ in 0..MAX_THREADS
{
let q=Arc::clone(&q);
let handle = thread::spawn(move || {
let q = Arc::clone(&q);
while(true)
{
let item = q.pop();
if item == None
{
return;
}
//do something with the item
println!("{} {}", item.as_ref().unwrap().0, item.as_ref().unwrap().1);
}
});
handles.push(handle);
}
for handle in handles
{
handle.join().unwrap();
}
}
fn main() {
let mut map=HashMap::new();
for i in 0..100
{
map.insert(
format!("Foo_{}", i),
i as f64
);
}
run(&map);
}