所以有简单的类
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/