c++ - boost 互斥顺序

标签 c++ multithreading boost locking

所以有简单的类

class mySafeData
{
public:
  mySafeData() : myData(0)
  {
  }

void Set(int i) 
  {
    boost::mutex::scoped_lock lock(myMutex);
    myData = i; // set the data
    ++stateCounter;  // some int to track state chages
    myCondvar.notify_all(); // notify all readers
  }

  void Get( int& i)
  {
    boost::mutex::scoped_lock lock(myMutex);
    // copy the current state
    int cState = stateCounter;
    // waits for a notification and change of state
    while (stateCounter == cState)
      myCondvar.wait( lock );
  }
 private:
   int myData;
   int stateCounter;
   boost::mutex myMutex;
};

无限循环中的线程数组调用每个函数

 Get()
 Set()
 Get()
 Get()
 Get()

他们是否总是以相同的顺序调用函数并且每个循环只调用一次(循环我的意思是所有 boost 线程每次都以相同的顺序运行,以便每个线程在之后仅执行一次 Get()一个 Set())?

最佳答案

没有。您永远不能对线程的服务顺序做出任何假设。这与 boost 无关,它是多道程序设计的基础。

关于c++ - boost 互斥顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4713782/

相关文章:

c++ - 非主线程中的QApplication

c++ - 在 switch 语句中通过引用返回

c++ - 将 boost::lambda 与 STL 容器结合使用

c# - 提供异步串口通信

c++ - std::thread --- 没有匹配的构造函数

c++ - 如何确定请求了哪个编译器

java - 如何正确关闭 javafx Alerts/fileChooser 等

c++ - 无法将 boost 安装到其他目​​录

c++ - boost asio 和条件变量——奇怪的输出

python - 无法使用 MSVC2015 编译 boost.python 1.65.1