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

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

View Full Document Right Arrow Icon
Today Assignment 4 Questions? Condition Variables (Quick Review) Semaphores Exercise – busy-wait vs. CV vs. Semaphores
Image of page 1

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

View Full Document Right Arrow Icon
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)
Image of page 2
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”)
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
Image of page 5
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

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern