【发布时间】:2011-01-17 13:41:48
【问题描述】:
所以有简单的类
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()
他们是否总是以相同的顺序调用函数,并且每个圆圈只调用一次(我的意思是所有提升线程每次都以相同的顺序运行,以便每个线程在一个 Set( ))?
【问题讨论】:
标签: c++ multithreading boost locking