class: title # Concurrency Bugs ## CSC 213 – October 26, 2026 --- # Agenda for Today 1. Department Events 1. Upcoming Work 1. Concurrency Bugs 1. Wrap Up --- # Department Events ## CS Table: Upgrade Holdouts and Going Analog .indent[ Tuesday, October 27th at noon in JRC 224C ] ## CS Extra: Student Presentations .indent[ Thursday, October 29th at 4pm in Noyce 3821 ] ## Faculty Candidate Talks .indent[ Coming soon. Watch for emails about timing and topics. ] *To receive a token, send an email to let me know you attended no more than 48 hours after the event.* --- # Election Day is Next Week If you are a US citizen, you can vote in Iowa as long as you aren't voting in a different state. Bring a photo ID (e.g. driver's license) with you when you vote. If you aren't registered already, you can register on election day. You can get a copy of a proof-of-residence letter from the Grinnell College housing portal. ## When do I vote? Tuesday, November 3rd from 7am—8pm Or vote early today from 9am-3pm in JRC 101 ## Where do I vote? Find your polling place at
## What are we voting for? You can find a sample ballot for your ward at
See full details on registration and elections at
. --- class: section, blue # Upcoming Work --- # Assignment: Concurrency Bugs **Due:** Monday, November 2nd by 11:59pm **Late Deadline:** Wednesday, November 4th by 10:00am This is another written assignment you'll submit directly on gradescope. Today's topics and discussion should prepare you to diagnose some concurrency bugs and propose solutions to each of them. --- # Office Hours I'm switching to drop-in hours only, at least for now. I will be available to meet at the following times: - Monday, 1:15–2:00pm - Tuesday, 1:30–3:00pm - Wednesday, 4:05–4:50pm I may need to limit your time during drop-in hours if there's a line, or I might try to combine groups if there are many students with similar questions. As before, if none of these times work for you, send me an email and we can find an alternate time to meet. --- class: section, blue # Concurrency Bugs --- # Discussion: Concurrency Bugs **Questions for think, pair, share:** 1. What are the three types of concurrency bugs? Explain them in your own words. 2. What is one example for each of the three types of concurrency bugs? Try to be specific. 3. Which concurrency bug type do you think is the most difficult to diagnose or prevent? Why? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- class: section, gray # An Example Program --- class: section, black
--- # A Flag-Drawing Program .left-col[ ```c int row = 0; int color = RED; int main() { pthread_t ts[3]; for(int i=0; i<3; i++) { pthread_create(&ts[i], NULL, worker, NULL); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* p) { int my_row = row; row++; int my_color = color; if(color == RED) { color = WHITE; } else if(color == WHITE) { color = BLUE; } for(int col=0, col<4, col++) { put_color(my_row, col, my_color); } return NULL; } ``` ] --- class: section, black
--- class: section, black
--- class: section, black
--- class: section, black
--- # What went wrong? .left-col[ ```c int row = 0; int color = RED; int main() { pthread_t ts[3]; for(int i=0; i<3; i++) { pthread_create(&ts[i], NULL, worker, NULL); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* p) { int my_row = row; row++; int my_color = color; if(color == RED) { color = WHITE; } else if(color == WHITE) { color = BLUE; } for(int col=0, col<4, col++) { put_color(my_row, col, my_color); } return NULL; } ``` ] --- # A Better Flag-Drawing Program .left-col[ ```c int color = RED; int main() { pthread_t ts[3]; int rows[3]; for(int i=0; i<3; i++) { rows[i] = i; pthread_create(&ts[i], NULL, worker, &rows[i]); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* arg) { int my_row = *(int*)arg; int my_color = color; if(color == RED) { color = WHITE; } else if(color == WHITE) { color = BLUE; } for(int col=0, col<4, col++) { put_color(my_row, col, my_color); } return NULL; } ``` ] --- class: section, black
--- class: section, black
--- class: section, black
--- class: section, black
--- # What went wrong? .left-col[ ```c int color = RED; int main() { pthread_t ts[3]; int rows[3]; for(int i=0; i<3; i++) { rows[i] = i; pthread_create(&ts[i], NULL, worker, &rows[i]); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* arg) { int my_row = *(int*)arg; int my_color = color; if(color == RED) { color = WHITE; } else if(color == WHITE) { color = BLUE; } for(int col=0, col<4, col++) { put_color(my_row, col, my_color); } return NULL; } ``` ] --- # A Better Flag-Drawing Program .left-col[ ```c int color = RED; pthread_mutex_t color_lock = PTHREAD_MUTEX_INITIALIZER; int main() { pthread_t ts[3]; int rows[3]; for(int i=0; i<3; i++) { rows[i] = i; pthread_create(&ts[i], NULL, worker, &rows[i]); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* arg) { int row = *(int*)arg; pthread_mutex_lock(&color_lock); int my_color = color; if(color == RED) { color = WHITE; } else if(color == WHITE) { color = BLUE; } pthread_mutex_unlock(&color_lock); for(int col=0, col<4, col++) { put_color(row, col, my_color); } return NULL; } ``` ] --- class: section, black
--- class: section, black
--- class: section, black
--- class: section, black
--- # What went wrong? .left-col[ ```c int color = RED; pthread_mutex_t color_lock = PTHREAD_MUTEX_INITIALIZER; int main() { pthread_t ts[3]; int rows[3]; for(int i=0; i<3; i++) { rows[i] = i; pthread_create(&ts[i], NULL, worker, &rows[i]); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* arg) { int row = *(int*)arg; pthread_mutex_lock(&color_lock); int my_color = color; if(color == RED) { color = WHITE; } else if(color == WHITE) { color = BLUE; } pthread_mutex_unlock(&color_lock); for(int col=0, col<4, col++) { put_color(row, col, my_color); } return NULL; } ``` ] --- # One Possible Fix .left-col[ ```c int main() { pthread_t ts[3]; int rows[3]; for(int i=0; i<3; i++) { rows[i] = i; pthread_create(&ts[i], NULL, worker, &rows[i]); } for(int i=0; i<3; i++) { pthread_join(threads[i], NULL); } } ``` ] .right-col[ ```c void* worker(void* arg) { int row = *(int*)arg; int my_color; if (row == 0) { my_color = RED; } else if (row == 1) { my_color = WHITE; } else { my_color = BLUE; } for(int col=0, col<4, col++) { put_color(my_row, col, my_color); } return NULL; } ``` ] --- class: section, blue # Deadlock --- # Discussion: Deadlock **Questions for think, pair, share:** 1. What are the four conditions required for deadlock to occur? Explain what each one means in your own words. 2. How can we prevent each of these four conditions? 3. What do you think is the easiest way to prevent deadlock? What about the hardest? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- class: section, blue # Wrap Up --- # Reminders ## Upcoming Deadlines - **Worm Lab:** due tonight with a token - **Concurrency Bugs Assignment:** due next Monday, or Wednesday morning with a token ## Reading - **Review lab before class**