Buffer threadsafety with semaphores