Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve thread safety, performance, and maintainability of the MapUpdater class #1

Merged
merged 11 commits into from
Jan 2, 2025

Conversation

nl-saw
Copy link
Owner

@nl-saw nl-saw commented Jan 2, 2025

1. Atomic Operations for Thread Safety
The refactored MapUpdater class now uses atomic operations (fetch_add and fetch_sub) for managing the pending_requests counter. This improves thread safety without the need for locking in certain cases, reducing contention and improving performance.

Improvement: This change ensures that the modification of pending_requests can be done in a lock-free manner, which enhances concurrency and reduces the risk of race conditions.

2. Use of std::memory_order_acquire in Atomic Operations
In the refactored code, atomic operations (fetch_add and fetch_sub) are performed with the std::memory_order_acquire flag, which ensures proper synchronization between threads and avoids stale reads.

Improvement: This adds stronger memory ordering guarantees, ensuring that all memory accesses before the atomic operation are properly synchronized with subsequent operations.

3. Simplified wait Method with Condition Variable Predicate
The refactored wait method has been simplified to use a condition variable predicate instead of manually checking the pending_requests count in a while loop. The new code waits until pending_requests is 0, using the atomic counter.

Improvement: This enhances readability and clarity. It also ensures that the thread only wakes up when there are no pending requests, improving efficiency.

4. Introduction of schedule_task Helper Method
A new schedule_task method is introduced to centralize the logic for scheduling tasks, incrementing the pending_requests counter, and pushing tasks to the queue. This method is used by both schedule_update and schedule_lfg_update.

Improvement: This reduces code duplication and makes the scheduling logic more maintainable and extensible.

5. Conditional Notification in update_finished
The refactored update_finished method uses atomic decrement and condition variable notification only when pending_requests becomes zero. This ensures that the condition variable is notified only when all tasks are complete.

Improvement: This reduces unnecessary notifications, improving efficiency by avoiding spurious wakeups.

6. Thread Safety in WorkerThread
The WorkerThread function in the refactored code ensures that tasks are processed only if they are valid and the cancellation token is not set. It also handles cleanup (delete request) more clearly within the while loop.

Improvement: The thread management is more robust with better handling of the cancellation condition, reducing the chances of errors during task execution.

7. Initialization of _cancelationToken
The _cancelationToken is now initialized directly in the MapUpdater constructor, ensuring it has a valid state when the object is created.

Improvement: This eliminates potential issues where the cancellation token could have been used before initialization.

8. Clearer Comments and Code Organization
The refactored code includes better comments explaining the purpose of different methods, such as in deactivate, update_finished, and wait. It also better organizes logic related to task processing and synchronization.

Improvement: The code is more readable and easier to understand for future developers, improving maintainability.

@nl-saw nl-saw merged commit 1ea60d1 into mapupdater Jan 2, 2025
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