提升互斥量顺序

Rel*_*lla 1 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;
};
Run Code Online (Sandbox Code Playgroud)

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

 Get()
 Set()
 Get()
 Get()
 Get()
Run Code Online (Sandbox Code Playgroud)

他们总是以相同的顺序调用函数,每个循环只调用一次(通过循环,我的意思是每次都会以相同的顺序运行所有提升线程,这样每个线程只会在Get()之后只有一次Set())?

Jul*_*ano 5

不可以.您永远不能假设线程的服务顺序.这与增强无关,它是多道程序设计的基础.