use two critical sections for in and out