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

你好,关于代码,我有几个疑问想请教一下你。 #40

Open
tiankaijin opened this issue Jul 31, 2019 · 1 comment
Open

Comments

@tiankaijin
Copy link

tiankaijin commented Jul 31, 2019

  1. 请问函数void Server::handNewConn()里面的eventLoopThreadPool_->getNextLoop()会不会变有竞态,是否存在将accept_fd分配给一个已经挂掉的loop或thread,因为毕竟是多线程。
void Server::handNewConn()
{
    while((accept_fd = accept(listenFd_, (struct sockaddr*)&client_addr, &client_addr_len)) > 0)
    {
        EventLoop *loop = eventLoopThreadPool_->getNextLoop();
       ...
    }
    acceptChannel_->setEvents(EPOLLIN | EPOLLET);
}

2.EventLoop::runInLoop(Functor&& cb),每一个thread都有负责一个loop,loop在某一个thread里面一直循环。程序走到这里,一定是某个thread监听的fd有事件,那么这个if判断是否一定为真?

void EventLoop::runInLoop(Functor&& cb)
{
    if (isInLoopThread())
        cb();
    else
        queueInLoop(std::move(cb));
}

3.void HttpData::handleRead(),这个函数里面有这样一段代码,error判断了多次,注释里解释是error可能会变。我是这样想的:每个对于每一个accept_fd,都有一个channel,都有一个独立的httpdata实例,那error会什么会变呢?

if (!error_)
    {
        if (outBuffer_.size() > 0)
        {
            handleWrite();
        }
        // error_ may change
        if (!error_ && state_ == STATE_FINISH)
        {
            this->reset();
            if (inBuffer_.size() > 0)
            {
                if (connectionState_ != H_DISCONNECTING)
                    handleRead();
            }
        }
        else if (!error_ && connectionState_ != H_DISCONNECTED)
            events_ |= EPOLLIN;
    }

@linyacool
Copy link
Owner

@tiankaijin

  1. getNextLoop就一个线程调,不会有竞态。已经挂掉的loop或thread,线程一个挂掉整个进程就挂了。
  2. 不一定为真的,看是从哪个线程调的。
  3. handleWrite里面可能改了

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

No branches or pull requests

2 participants