Skip to content

EventLoopThread::startLoop()中为什么需要创建局部变量loop?是否可以返回loop_? #730

@lamb007

Description

@lamb007
EventLoop* EventLoopThread::startLoop()
{
  assert(!thread_.started());
  thread_.start();

  EventLoop* loop = NULL;
  {
    MutexLockGuard lock(mutex_);
    while (loop_ == NULL)
    {
      cond_.wait();
    }
    loop = loop_;
  }

  return loop;
}

void EventLoopThread::threadFunc()
{
  EventLoop loop;

  if (callback_)
  {
    callback_(&loop);
  }

  {
    MutexLockGuard lock(mutex_);
    loop_ = &loop;
    cond_.notify();
  }

  loop.loop();
  //assert(exiting_);
  MutexLockGuard lock(mutex_);
  loop_ = NULL;
}

EventLoopThread::startLoop()是否可以实现为下面的方式?

EventLoop* EventLoopThread::startLoop() {
  thread_.start();
  std::unique_lock lock(mutex_);
  cv_.wait(lock, [this]() { return loop_ != nullptr; });
  return loop_;
}

望赐教,谢谢

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions