工作线程暂停/恢复实现

时间:2017-09-04 08:21:17

标签: multithreading concurrency c++14 worker-thread

在我尝试向我的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.

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

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

1 个答案:

答案 0 :(得分:0)

当然Process()工作的suspend不必由j.executed互斥锁保护。

std::atomic<int> - 对断言以及递增 - 的访问确实需要同步(通过使其成为{{1}}或通过使用互斥锁等来保护它)。

它仍然不清楚为什么这个问题表明它的方式(因为我没有在主线程上写入变量) - 可能是undefined behaviour propagating backwards in time的情况。< / p>