Similar to the first, but synchronizes with semaphores