Added mutex to the flush function.