rec11-2010

# rec11-2010 - ● Decrement sem_wait Semaphores ● How many...

This preview shows pages 1–5. Sign up to view the full content.

Today Assignment 4 Questions? Condition Variables (Quick Review) Semaphores Exercise – busy-wait vs. CV vs. Semaphores

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Condition Variables What are condition variables? When should we use them? Describe the following functions in your own words: pthread_cond_wait(cv,mutex) pthread_cond_signal(cv) pthread_cond_broadcast(cv)
Semaphores Semaphores are another synchronization method other than mutex locking. Think of a semaphore as a special kind of integer: It can be incremented or decremented atomically. If the value of the semaphore is 0, it cannot be decremented. Threads that try to decrement it when its value is 0 go to sleep. Increment: sem_post (like “signal”)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ● Decrement: sem_wait Semaphores ● How many threads can access the “critical section” of code below? sem_t accesslock; sem_init(&access_lock, 0, 2); // thread code: sem_wait(&access_lock); // critical section sem_post(&access_lock); Exercise ● Synchronize our program that uses two threads to print out alternating “+-+-+-”. One thread prints out “+”, the other “-”. ● We have already implemented this for busy-waiting for you. Run this code to see how badly it performs. (Timers included in code) ● Use two new methods to synchronize this program and compare their performance with busy-wait: condition vars, semaphores...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern