overflow handling

This commit is contained in:
Robin Müller 2022-11-21 10:56:43 +01:00
parent 6b93a9fce1
commit e06cadd088
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 15 additions and 0 deletions

View File

@ -25,6 +25,10 @@ impl SequenceCountProvider<u16> for SimpleSeqCountProvider {
}
fn increment(&mut self) {
if self.seq_count == u16::MAX {
self.seq_count = 0;
return;
}
self.seq_count += 1;
}
}

View File

@ -1,5 +1,7 @@
#![allow(dead_code)]
use crossbeam_channel::{bounded, Receiver, Sender};
use std::sync::atomic::{AtomicU16, Ordering};
use std::thread;
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned, U16};
@ -52,4 +54,13 @@ fn main() {
let jh1 = thread::spawn(|| {});
jh0.join().unwrap();
jh1.join().unwrap();
//let mut max_val: u16 = u16::MAX;
//max_val += 1;
//println!("Max val: {}", max_val);
let atomic_u16: AtomicU16 = AtomicU16::new(u16::MAX);
atomic_u16.fetch_add(1, Ordering::SeqCst);
println!(
"atomic after overflow: {}",
atomic_u16.load(Ordering::SeqCst)
);
}