Skip to content

Fix thread safety issues in producer-consumer implementation#19238

Open
bkk07 wants to merge 1 commit into
eugenp:masterfrom
bkk07:fix-producer-consumer-thread-safety
Open

Fix thread safety issues in producer-consumer implementation#19238
bkk07 wants to merge 1 commit into
eugenp:masterfrom
bkk07:fix-producer-consumer-thread-safety

Conversation

@bkk07
Copy link
Copy Markdown

@bkk07 bkk07 commented Jun 1, 2026

This PR fixes thread-safety issues in the producer-consumer implementation.

The previous implementation accessed a shared LinkedList concurrently without proper synchronization. Queue condition checks and wait/notify coordination were performed outside a shared lock, which could lead to race conditions, missed notifications, visibility issues, and potential queue corruption when multiple producers and consumers executed concurrently.

Changes made:

  • Moved synchronization responsibility into DataQueue
  • Guarded queue operations using synchronized methods
  • Replaced unsafe condition checks with guarded wait loops
  • Removed separate wait/notify coordination from Producer and Consumer
  • Added proper handling for spurious wakeups using while loops
  • Added volatile visibility guarantees for running flags

This preserves the original educational intent of demonstrating wait/notify based producer-consumer coordination while ensuring thread-safe behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant