class: title # Semaphores and Condition Variables ## CSC 213 – October 27, 2025 --- # Agenda for Today 1. Department Events 2. Condition Variables 3. Semaphores 4. Wrap Up --- # Department Events ## CS Table: Topic TBD .indent[ Tuesday, October 28th at noon in JRC 224C ] ## CS Poster Session .indent[ Thursday, October 30th at 4:30pm in HSSC A1231 Fruit, desserts, and beverages provided. ] *To receive 0.25% extra credit, send an email to let me know you attended no more than 48 hours after the event.* --- class: section, blue # Condition Variables --- # Discussion: Condition Variable Basics **Questions for think, pair, share:** 1. What does a condition variable do? 2. Why would we choose to use a condition variable with a mutex instead of just a mutex? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- # Discussion: Condition Variable Interface **Questions for think, pair, share:** For each of the condition variable functions below, explain what the function does and when you would use it. 1. `pthread_cond_wait(pthread_cond_t* cv, pthread_mutex_t* m)` 2. `pthread_cond_signal(pthread_cond_t* cv)` 3. `pthread_cond_broadcast(pthread_cond_t* cv)` *Start by thinking of answers on your own. I will let you know when to start discussion.* --- # Exercise: A Simple Parallel Program .left-col[ ## Main Function - Create two threads, tick_thread and tock_thread - While there is input from the user: - If the user typed "go", both threads are allowed to move forward - Otherwise, wait for more input. - Join with tick_thread and tock_thread ] .right-col[ ## Thread Function - Wait for the main thread to allow this thread to run. - Repeat five times: - Print this thread's message (either "tick" or "tock") - Pause for one second ]
## How should we make the threads wait, and how do we resume them? Discuss strategies with your group. We'll complete the implementation together. --- class: section, gray # Implementing Tick/Tock with Condition Variables --- class: section, blue # Semaphores --- # Discussion: Semaphore Basics **Questions for think, pair, share:** 1. What does a semaphore do? 2. How is a semaphore different from a mutex or condition variable? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- # Discussion: Semaphore Interface **Questions for think, pair, share:** For each of the semaphore functions below, explain what the function does and when you would use it. 1. `sem_init(sem_t* s, int cross_process, int state)` 2. `sem_wait(sem_t* s)` 3. `sem_post(sem_t* s)` *Start by thinking of answers on your own. I will let you know when to start discussion.* --- # Exercise: A Simple Parallel Program .left-col[ ## Main Function - Create two threads, tick_thread and tock_thread - While there is input from the user: - If the user typed "go", both threads are allowed to move forward - Otherwise, wait for more input. - Join with tick_thread and tock_thread ] .right-col[ ## Thread Function - Wait for the main thread to allow this thread to run. - Repeat five times: - Print this thread's message (either "tick" or "tock") - Pause for one second ]
## How can we implement this same program with semaphores? Discuss strategies with your group. We'll complete the implementation together. --- class: section, gray # Implementing Tick/Tock with Semaphores --- class: section, blue # Wrap Up --- # Reminders ## Lab You can submit the worm lab for up to 80% credit until this Wednesday. ## Assignment There is no assignment this week. ## Reading - **Review lab before class**