Multithreading with mutual exclusion