我只是在尝试在结构上实现原子读/写时寻找反馈(明显的缺陷/改进方法)。
将有一个写线程和多个读线程。目的是防止读者对结构产生不一致的看法,同时不要过多地阻碍作者。
我正在使用fetch-and-add atomic原语,在这种情况下由Qt框架提供。
例如:
/* global */
OneWriterAtomicState<Point> atomicState;
/* Writer */
while(true) {
MyStruct s = atomicState.getState()
s.x += 2; s.y += 2;
atomicState.setState(s);
}
/* Reader */
while(true) {
MyStruct s = atomicState.getState()
drawBox(s.x,s.y);
}
OneWriterAtomicState实施:
template <class T>
class OneWriterAtomicState
{
public:
OneWriterAtomicState()
: seqNumber(0)
{
}
void setState(T& state) {
this->seqNumber.fetchAndAddOrdered(1);
this->state = state;
this->seqNumber.fetchAndAddOrdered(1);
}
T getState(){
T result;
int seq;
bool seq_changed = true;
/* repeat while seq is ODD or if seq changes during read operation */
while( (seq=this->seqNumber.fetchAndAddOrdered(0)) & 0x01 || seq_changed ) {
result = this->state;
seq_changed = (this->seqNumber.fetchAndAddOrdered(0)!=seq);
}
return result;
}
private:
QAtomicInt seqNumber;
T state;
}
这是第二版(memcpy,读者屈服,希望修复getState()):
template <class T>
class OneWriterAtomicState
{
public:
OneWriterAtomicState()
: seqNumber(0)
{
/* Force a compile-time error if T is NOT a type we can copy with memcpy */
Q_STATIC_ASSERT(!QTypeInfo<T>::isStatic);
}
void setState(T* state) {
this->seqNumber.fetchAndAddOrdered(1);
memcpy(&this->state,state,sizeof(T));
this->seqNumber.fetchAndAddOrdered(1);
}
void getState(T* result){
int seq_before;
int seq_after = this->seqNumber.fetchAndAddOrdered(0);
bool seq_changed = true;
bool firstIteration = true;
/* repeat while seq_before is ODD or if seq changes during read operation */
while( ((seq_before=seq_after) & 0x01) || seq_changed ) {
/* Dont want to yield on first attempt */
if(!firstIteration) {
/* Give the writer a chance to finish */
QThread::yieldCurrentThread();
} else firstIteration = false;
memcpy(result,&this->state,sizeof(T));
seq_after = this->seqNumber.fetchAndAddOrdered(0);
seq_changed = (seq_before!=seq_after);
}
}
bool isInitialized() { return (seqNumber>0); }
private:
QAtomicInt seqNumber;
T state;
} ;
#endif // ONEWRITERATOMICSTATE_H
答案 0 :(得分:2)
算法不太正确。这是一个可能的线程交错,读者获得不一致的数据:
state initialized to {0,0} and seqNumber to 0
Writer:
seqNumber = 1;
state.x = 1;
Reader:
seq = seqNumber; //1
result = state; //{1,0}
seq_changed = (seqNumber != seq); //false
Writer:
state.y = 1;
seqNumber = 2;
Reader:
jumps back to the start of the loop
seq = seqNumber; //2
steps out of the loop because seq == 2 and seq_changed == false
所以问题是在两个地方读取seqNumber
并且编写者可以更新读取之间的值。
while( (seq=this->seqNumber.fetchAndAddOrdered(0)) & 0x01 || seq_changed ) {
result = this->state;
seq_changed = (this->seqNumber.fetchAndAddOrdered(0)!=seq);
//If writer updates seqNumber here to even number bad things may happen
}
每次迭代只应读取一次:
T getState(){
T result;
int seq;
int newseq = seqNumber.fetchAndAddOrdered(0);
bool seq_changed = true;
while( (seq = newseq) & 0x01 || seq_changed ) {
result = state;
newseq = seqNumber.fetchAndAddOrdered(0);
seq_changed = (newseq != seq);
}
return result;
}
我相信这应该正常,但我不保证任何事情。 :)至少你应该编写一个测试程序,就像你的例子中的一个,但在读者中添加一个不一致值的检查。
值得考虑的一件事是使用原子增量(fetchAndAdd)有点过分。只有一个线程写seqNumber
,因此您可以使用简单的原子存储释放和负载获取操作,并且可以在许多处理器上更快地实现它们。但是我不知道QAtomicInt
是否可以进行这些操作;文件很不清楚。
编辑:和wilx是对的,T需要是一个简单的可复制类型
答案 1 :(得分:1)
我认为只有T
的复制赋值运算符是原始的并且基本上只进行按位复制时才会有效。对于任何更复杂的T
,您最终可能会在执行result = this->state;
期间获得不一致的状态。
所以,我建议使用某种具有作家偏好的rwlock。
答案 2 :(得分:1)
如果您有基于优先级的线程调度,并且阅读器的优先级高于编写器,则可能会遇到活锁。想象一下,作者开始写入价值,然后读者进入主动等待。由于读者的优先级较高,作者永远不会有机会完成写作。
解决方案是在等待循环中添加一个小延迟。