Skip to content

Fix thread safety issues in producer-consumer implementation#19238

Merged
lor6 merged 2 commits into
eugenp:masterfrom
bkk07:fix-producer-consumer-thread-safety
Jul 22, 2026
Merged

Fix thread safety issues in producer-consumer implementation#19238
lor6 merged 2 commits into
eugenp:masterfrom
bkk07:fix-producer-consumer-thread-safety

Conversation

@bkk07

@bkk07 bkk07 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

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.

@lor6

lor6 commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks @bkk07 The PR looks good, but the fork is outdated so I'm not able to merge it. Could you update your fork with the latest changes from origin?

@bkk07

bkk07 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Hi @lor6,
I've updated my branch with the latest changes from upstream/master as requested. All checks are now passing. Could you please take another look when you have a chance?
Thank you!

@lor6
lor6 merged commit df51f86 into eugenp:master Jul 22, 2026
1 check passed
@lor6

lor6 commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks @bkk07 I've now merged the PR here. I will check what changes are needed in the related article.

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.

2 participants