Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void read_lock()
- {
- //Locks the mutex
- pthread_mutex_lock(&lock);
- while (writerWaiting)
- {
- pthread_cond_wait(&cond, &lock);
- }
- ++readersWaiting;
- }
- void read_unlock()
- {
- --readersWaiting;
- //IMPL1
- while (readersWaiting > 0)
- {
- pthread_cond_wait(&cond, &lock);
- }
- pthread_cond_signal(&cond);
- pthread_mutex_unlock(&lock);
- }
- void write_lock()
- {
- pthread_mutex_lock(&lock);
- while (writerWaiting)
- {
- pthread_cond_wait(&cond, &lock);
- }
- }
- void write_unlock()
- {
- writerWaiting = true;
- while (readersWaiting > 0)
- {
- pthread_cond_wait(&cond, &lock);
- }
- writerWaiting = false;
- pthread_cond_broadcast(&cond);
- pthread_mutex_unlock(&lock);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement