Added semaphor to avoid write collision in different threads