1

在我尝试向我的 Worker [thread] 类添加挂起/恢复功能时,我遇到了一个我无法解释的问题。(C++1y / VS2015)

该问题看起来像一个死锁,但是一旦附加了调试器并且在某个点之前设置了断点(请参阅#1),我似乎无法重现它 - 所以它看起来像是一个时间问题。

我可以找到的修复(#2)对我来说没有多大意义,因为它需要更长时间地持有互斥锁,并且客户端代码可能会尝试获取其他互斥锁,我理解这实际上增加了僵局。

但它确实解决了这个问题。

工人循环:

Job* job;
while (true)
{
  {
    std::unique_lock<std::mutex>  lock(m_jobsMutex);
    m_workSemaphore.Wait(lock);

    if (m_jobs.empty() && m_finishing)
    {
      break;
    }

    // Take the next job
    ASSERT(!m_jobs.empty());
    job = m_jobs.front();
    m_jobs.pop_front();
  }

  bool done = false;
  bool wasSuspended = false;
  do
  {
    // #2
    { // Removing this extra scoping seemingly fixes the issue BUT
      // incurs us holding on to m_suspendMutex while the job is Process()ing,
      // which might 1, be lengthy, 2, acquire other locks.
      std::unique_lock<std::mutex> lock(m_suspendMutex);
      if (m_isSuspended && !wasSuspended)
      {
        job->Suspend();
      }
      wasSuspended = m_isSuspended;

      m_suspendCv.wait(lock, [this] {
        return !m_isSuspended;
      });

      if (wasSuspended && !m_isSuspended)
      {
        job->Resume();
      }
      wasSuspended = m_isSuspended;
    }

    done = job->Process();
  }
  while (!done);
}

暂停/恢复只是:

void Worker::Suspend()
{
  std::unique_lock<std::mutex>  lock(m_suspendMutex);
  ASSERT(!m_isSuspended);
  m_isSuspended = true;
}

void Worker::Resume()
{
  {
    std::unique_lock<std::mutex>  lock(m_suspendMutex);
    ASSERT(m_isSuspended);
    m_isSuspended = false;
  }
  m_suspendCv.notify_one(); // notify_all() doesn't work either.
}

(Visual Studio)测试:

  struct Job: Worker::Job
  {
    int durationMs = 25;
    int chunks = 40;
    int executed = 0;

    bool Process()
    {
      auto now = std::chrono::system_clock::now();
      auto until = now + std::chrono::milliseconds(durationMs);
      while (std::chrono::system_clock::now() < until)
      { /* busy, busy */
      }

      ++executed;
      return executed < chunks;
    }

    void Suspend() { /* nothing here */ }
    void Resume() { /* nothing here */ }
  };

  auto worker = std::make_unique<Worker>();

  Job j;
  worker->Enqueue(j);

  std::this_thread::sleep_for(std::chrono::milliseconds(j.durationMs));  // Wait at least one chunk.

  worker->Suspend();

  Assert::IsTrue(j.executed < j.chunks);  // We've suspended before we finished.
  const int testExec = j.executed;

  std::this_thread::sleep_for(std::chrono::milliseconds(j.durationMs * 4));

  Assert::IsTrue(j.executed == testExec); // We haven't moved on.

  // #1
  worker->Resume(); // Breaking before this call means that I won't see the issue.
  worker->Finalize();

  Assert::IsTrue(j.executed == j.chunks); // Now we've finished.

我错过了什么/做错了什么?为什么作业的 Process()ing 必须由suspend互斥锁保护?

编辑Resume()在通知时不应该一直持有互斥锁;这是固定的 - 问题仍然存在。

4

1 回答 1

0

当然Process(),工作的 ing 不必由suspend互斥锁来保护。

但是,j.executed对于断言和递增的访问 - 但是确实需要同步(通过使其成为一个std::atomic<int>或通过使用互斥锁等来保护它)。

目前尚不清楚为什么问题会以这种方式表现出来(因为我没有写入主线程上的变量) - 可能是未定义行为及时向后传播的情况

于 2017-09-06T21:30:21.900 回答