Skip to content

Optimize EventReactor thread safety with shared_mutex for better concurrency - #1

Merged
lmshao merged 1 commit into
masterfrom
copilot/fix-e7188ad1-6f46-43f9-bca7-929d8b4bc477
Jul 6, 2025
Merged

Optimize EventReactor thread safety with shared_mutex for better concurrency#1
lmshao merged 1 commit into
masterfrom
copilot/fix-e7188ad1-6f46-43f9-bca7-929d8b4bc477

Conversation

Copilot AI commented Jul 6, 2025

Copy link
Copy Markdown
Contributor

Problem

The EventReactor class was using std::mutex for all operations, creating unnecessary lock contention in high-concurrency scenarios. Read operations like DispatchEvent() and handler lookups were blocking each other even though they could safely run concurrently.

Solution

Replaced the thread protection mechanism from std::mutex to std::shared_mutex with read-write lock semantics:

Changes Made

  1. Header Updates (event_reactor.h):

    • Added #include <shared_mutex>
    • Changed std::mutex mutex_ to std::shared_mutex mutex_
  2. Read Operations (use std::shared_lock for concurrent access):

    • DispatchEvent() - Multiple threads can now dispatch events concurrently
    • Run() method - Handler lookups can happen in parallel
    • ModifyHandler() - Only checks handler existence, no modification needed
  3. Write Operations (use std::unique_lock for exclusive access):

    • RegisterHandler() - Exclusive access when adding handlers
    • RemoveHandler() - Exclusive access when removing handlers

Performance Benefits

  • Concurrent Event Dispatching: Multiple threads can now process events simultaneously without blocking each other
  • Reduced Lock Contention: Read operations no longer compete with each other for lock acquisition
  • Better Scalability: Performance scales better with increased concurrent load

Example Impact

// Before: All operations serialized
std::unique_lock<std::mutex> lock(mutex_);  // Blocks all other operations

// After: Read operations can run concurrently
std::shared_lock<std::shared_mutex> lock(mutex_);  // Multiple readers allowed

Testing

  • ✅ All existing unit tests pass
  • ✅ TCP echo server example runs correctly
  • ✅ No functional regressions detected
  • ✅ Thread safety maintained

This optimization is particularly beneficial for high-throughput network applications where multiple events need to be processed concurrently.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@lmshao
lmshao marked this pull request as ready for review July 6, 2025 16:01
Copilot AI review requested due to automatic review settings July 6, 2025 16:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.

@lmshao
lmshao merged commit 51ce263 into master Jul 6, 2025
2 checks passed
Copilot AI changed the title [WIP] 优化 EventReactor 的线程保护机制 Optimize EventReactor thread safety with shared_mutex for better concurrency Jul 6, 2025
Copilot AI requested a review from lmshao July 6, 2025 16:05
Copilot finished work on behalf of lmshao July 6, 2025 16:05
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.

3 participants