pEnd_ 是一个对象成员,只能在一个线程中的 add() 中增加,如下所示,它可能被另一个线程读取。我们需要在 add() 中使用 __sync_synchronize 吗?
struct Vector {
...
void add(int v) {
*pEnd_ = v;
__sync_synchronize(); // is this needed?
++pEnd_;
}
private:
int* pBegin_;
int* pEnd_;
}
在另一个线程中迭代。
for (p = pBegin_; p != pEnd_; ++p) {
// read barrier here may be inserted
if (*p) {
....
}
}