views:

102

answers:

1

I have several modifying threads and some reading threads, which all access the global variable X. I want to make my synchronization policy like this:

When a thread try to modify X, it will require a lock first, and several modifying threads can have several locks required.

When a thread try to read X, it must wait until all the modifying threads drop their locks.

Is there any solution to this situation in linux pthread library? Many thanks

+2  A: 

You are looking for a read/write lock (or reader-writer lock). I believe there is one in pthreads (pthread_rwlock_*).

JP Alioto