【问题标题】:Right way to concurrently process std::stack并发处理 std::stack 的正确方法
【发布时间】:2017-11-23 20:34:44
【问题描述】:

这可能是多线程编程方面的一个基本问题,但我真的想在没有任何并发​​数据结构的情况下实现以下目标。 考虑代码:

class A
{
    std::stack<int> s;
public:
    A()
    {
        s.push(7); s.push(6); s.push(5); s.push(4); s.push(3); s.push(2); s.push(1);
    }
    void process(int tid)
    {
        while (!s.empty())
        {
            std::unique_lock<std::mutex> lck(m);
            std::cout << tid << " --> " << s.top() << '\n';
            cv.wait(lck);
            s.pop();
            cv.notify_all();
            lck.unlock();
        }
    }
    std::mutex m;
    std::condition_variable cv;
};

int main()
{   
    A a;
    std::thread t1(&A::process, &a, 1);
    std::thread t2(&A::process, &a, 2);    
    t1.join();
    t2.join();
}

我希望每个线程都打印堆栈的顶部并将其弹出,以便输出看起来像这样:

1 --> 1
2 --> 2
1 --> 3
2 --> 4
...

所以只有 1 个线程应该进入 while 主体并只执行一次迭代。

但它总是输出:

1 --> 1
2 --> 1 

然后无限等待

我该怎么做?

目前的解决方案有什么问题?

【问题讨论】:

  • 我不知道这是否是您唯一的问题,但您永远不应该等待没有循环或 lambda 的条件变量。啊,您似乎也认为条件变量是某种信号量?或者类似的。
  • @Yakk 我宁愿考虑使用条件变量来提供一些同步,以便线程只执行循环的 1 次迭代

标签: c++ multithreading c++11 thread-synchronization


【解决方案1】:

永远不要在没有测试虚假唤醒的情况下对条件变量执行wait。最简单的方法是使用 lambda 版本。

condition_variables 不是信号量,它们比信号量低。

class A
{
public:
  A()
  {
    s.push(7); s.push(6); s.push(5); s.push(4); s.push(3); s.push(2); s.push(1);
  }
  void process(int tid)
  {
    while (true)
    {
      std::unique_lock<std::mutex> lck(m);
      cv.wait(lck, [&]{ return std::this_thread::get_id() != last || s.empty(); });
      // must only read within lock:
      if (s.empty()) {
        last = std::thread::id{}; // thread ids can be reused
        break;
      }
      last = std::this_thread::get_id();
      std::cout << tid << " --> " << s.top() << '\n';
      s.pop();
      cv.notify_one();
    }
  }
  std::mutex m;
  std::condition_variable cv;
  std::thread::id last{};
  std::stack<int> s;
};

【讨论】:

  • 是的,我理解,但无论如何它不能以正确的方式工作。尝试从空容器中弹出时抛出异常...
  • @ampawd 我认为已修复
猜你喜欢
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 2011-02-28
  • 2016-01-15
  • 1970-01-01
  • 2016-05-02
相关资源
最近更新 更多