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

slave thread pool #5

Closed
wants to merge 4 commits into from

Conversation

panlei-coder
Copy link
Collaborator

add lock

@@ -85,9 +85,15 @@ class EventLoop {
// for unittest only
void Reset();

std::shared_ptr<EventObject> GetEventObject(int id){ return objects_[id]; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

用find方法,找不到返回null

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

好的

@@ -85,9 +85,15 @@ class EventLoop {
// for unittest only
void Reset();

std::shared_ptr<EventObject> GetEventObject(int id){ return objects_[id]; }
bool IsSlaveEventLoop(){ return name_.find("slave") != std::string::npos; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

待评估吧,把业务逻辑加到eventloop这样的底层类,不是太妥。

@@ -85,9 +85,15 @@ class EventLoop {
// for unittest only
void Reset();

std::shared_ptr<EventObject> GetEventObject(int id){ return objects_[id]; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

objects_[id] 这种方式获取 map内的数据, 如果map中没有这个id的数据, 会插入一条key 为这个id的数据, 所以在获取map数据时, 不要使用 objects_[id] 这种方式

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok

@@ -102,26 +106,29 @@ EventLoop* EventLoop::Self() { return g_this_loop; }
bool EventLoop::Register(std::shared_ptr<EventObject> obj, int events) {
if (!obj) return false;

assert(InThisLoop());
// assert(InThisLoop());
Copy link
Collaborator

Choose a reason for hiding this comment

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

为啥注释掉了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这个在进行TcpConnection从一个loop转移到另一个slave_loop时,会存在跨线程注册,这里对objects_作加锁保护了

} while (id < 0 || objects_.count(id) != 0);
{
// alloc unique id
std::unique_lock<std::mutex> guard(object_mutex_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

这行代码可以挪到 line 127 之下,以缩小 lock scope。毕竟119-125是原子操作。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

125行有对objects_进行访问,不应需要在while循环之前加锁么

@AlexStocks AlexStocks changed the base branch from main to unstable October 11, 2023 08:26
@panlei-coder
Copy link
Collaborator Author

The slave thread pool has resubmitted a new pr that fixes some of the previous irregularities

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.

None yet

4 participants