class: title # Semaphores and Condition Variables ## CSC 213 – October 14, 2026 --- # Agenda for Today 1. Department Events 1. Upcoming Work 1. Condition Variables 1. Semaphores 1. Wrap Up --- # Department Events ## CS Extra: Graduate School in CS .indent[ Thursday, October 15th at 4pm in Noyce 3821 Refreshments provided ] *To receive a token, send an email to let me know you attended no more than 48 hours after the event.* --- class: section, blue # Upcoming Work --- # Lab: Worm **Due:** Wednesday, October 14th by 11:59pm **Late Deadline:** Monday, October 26th by 11:59pm ## Questions _No new questions since last class._ --- 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 ## Upcoming Deadlines - **Worm Lab:** due tonight, or Monday after break with a token ## Need help? Use your resources! - Book an appointment during office hours - Attend mentor office hours - Review slides, readings, and other course resources - Use tools like `gdb` and AddressSanitizer to catch and diagnose issues ## Reading *No reading for the next class.*