{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Applications of Semaphores1

Applications of Semaphores1 - Applications of Semaphores N...

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

View Full Document Right Arrow Icon
Applications of Semaphores: N- join Assume N threads will block when calling njoin() until n threads call njoin(). njoin()
Image of page 1

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

View Full Document Right Arrow Icon
N-Join Class NJoin { int _n; int _ncurrent; mutex_lock _mutex; sema_t _sem; public: NJoin(int n); void join(); }; NJoin::NJoin(int n) { _n = n; _ncurrent = 0; Void Njoin::join() { mutex_lock(); _ncurrent++; if ( _ncurrent < _n) { // Need to wait mutex_unlock(&_mutex); sema_wait(&_sem); } else { // nth thread. // Wakeup n-1 threads for (int i=0; i<_n-1; i++) { sema_post(&_sem); } _ncurrent=0; mutex_unlock(&_mutex);
Image of page 2
Condition Variables Condition Variables is a mechanism used for synchronization like semaphores. Condition variables and semaphores are equivalent in the sense that they can solve the same problems of synchronization Condition variables are more expressive than semaphores but using one vs. the other is a matter of taste.
Image of page 3

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

View Full Document Right Arrow Icon
Using Condition Variables Condtion variables are used together with a mutex. cond_t cond_var; Condition variables are used in the following construction: mutex_lock(&mutex); while( condition_expression ){ cond_wait(&cond_var, &mutex); } // Do something. Mutex is being held.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the 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