Lecture Note 7
- Arc: Atomic reference counting
- considering concurrncy
Suppose one thread incrementing the value and second thread decrementing the value
l = Read the variable counter; write l + 1;l = Read the variable counter; write l - 1;if second thread is executed in between the first thread:
Rc: not considering concurrent runs Solving the problem ->Arc: guarantees no instructions scheduling interleaves the instruction.Atomic { l = read teh variable; write l + 1; } // no other thread interleaves // but not guaranteeing the order of the thread executionSend trait: itβs implementation is unsafe!
Cloning arc pointer: incrementing the reference counter when arc pointer is dropped: incrementing the reference counter
Arc::new(Mutex::new(0));
// arc guarantees the data inside is shared
// mutex guarantees the integrity of modification.
g = a.locK();
*g = 42;
-
spanwed thread panics -> delivered to main thread
- lifetime of function sent to spawn
let counter = Arc::clone(&counter); thread::spwan(move || { // use counter; // arc guarantees that counter will not be dropped; }) - Intermittent computing
- Implementing garbage collection:
- mark and sweep
- tracing algorihthm
- reference counting
- improved algorithm: reference count for small counts, tracing for larger counts
- reduce the $#$ of tracing (improve performance)
- problem of reference counting:
- cycled objects : there will be always an object that points to self -> use trace
-splitting the counts (strong / weak)
- tree : strong edge (going to child) / weak edge (going to parent)
- strong count -> drop
- weak reference