use std::thread;
#[derive(Clone)]
-struct ConcurrentCounter(Arc<RwLock<usize>>);
+pub struct ConcurrentCounter(Arc<RwLock<usize>>);
impl ConcurrentCounter {
// The constructor should not be surprising.
*counter = *counter + by;
}
+ pub fn compare_and_inc(&self, test: usize, by: usize) {
+ let mut counter = self.0.write().unwrap();
+ if *counter == test {
+ *counter += by;
+ }
+ }
+
pub fn get(&self) -> usize {
let counter = self.0.read().unwrap();
*counter