fix: resolve thread safety issues in simulation engine#20
Merged
Conversation
- Make static RNG thread_local in Organism::makeMove() and Genes::defaultMutationLogic() - Pre-compute organism list once before thread dispatch instead of per-thread - Skip thread creation overhead when numThreads=1 - Make handleInteractions single-threaded (mutates shared food/organism state) - Keep handleReactions multi-threaded (only writes to own organism's movement) - Use std::atomic<FoodState> for thread-safe food state transitions - Use safe objectsMapper.find() instead of operator[] in threaded context - Fix shared_ptr passed by value in calculateDistance (now const ref) - Make react()/interact() take const vector references - Remove dead removeDeadOrganisms() declaration - Remove commented-out debug printf statements Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
thread_localinOrganism::makeMove()andGenes::defaultMutationLogic()to fix data racesgetAllOrganisms()per-thread (was doing N expensivedynamic_pointer_castscans)std::threadcreation overhead whennumThreads=1handleInteractionssingle-threaded since it mutates shared state (food->eaten(),organism->killed(),lifeSpanmodifications)handleReactionsmulti-threaded (safe: each thread only writes to its own organism'smovement/reactionCounter)std::atomic<FoodState>for thread-safe food state transitionsobjectsMapper.find()instead ofoperator[]in threaded contextshared_ptrpassed by value incalculateDistance(now const ref)react()/interact()take const vector referencesremoveDeadOrganisms()declarationDiscussion Points
handleInteractions目前改為 single-threaded 來避免 data race。如果未來需要平行化 interaction phase,可以考慮用 per-object lock 或將 interaction 結果收集後 batch applyFood::canBeEaten()用std::atomic<FoodState>但兩個 organism 仍可能同時看到FRESH然後都加 energy。目前這是 acceptable 因為 interaction phase 已改為 single-threadedTest plan
numThreads > 1to verify reaction phase parallelism still works🤖 Generated with Claude Code