mit so einfachen KlasseBoost-Mutex um
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;
};
und Anordnung von Fäden in Endlosschleifen ruft jede eine Funktion
Get()
Set()
Get()
Get()
Get()
werden sie immer Funktionen in der gleichen Reihenfolge aufrufen und nur einmal pro Kreis (by circle Ich meine, werden alle Boost-Threads in der gleichen Reihenfolge jedes Mal so laufen, dass jeder Thread würde Get
() nur einmal nach einem Set
())?