class: title # Concurrency Bugs ## CSC 213 – October 31, 2025 --- # Agenda for Today 1. Department Events 2. Upcoming Work 3. Concurrency Bugs 4. Wrap Up --- # Department Events ## CS Table: Topic TBD .indent[ Tuesday, November 4th at noon in JRC 224C ] ## Faculty Candidate Talks .indent[ Coming soon. Watch for emails about timing and topics. ] *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 # Upcoming Work --- # Lab: Password Cracker **Due:** Wednesday, November 5th by 11:59pm **Late Deadline:** Wednesday, November 12th by 11:59pm ## Q&A _No general questions so far._ --- 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 ## Lab The password cracker lab is due next week. Make sure you've arranged times to work with your group. You might want to schedule those times during upcoming mentor sessions. ## Assignment There is no assignment this week. ## Reading - **Introduction** (CUDA Programming Guide, Chapter 3) - **Programming Model** (CUDA Programming Guide, Section 5.1–5.3)