由于 concurrent_vector 不使用连续的内存块,我只想确定这是否可以:
concurrency::concurrent_vector<Something> my_array;
//Populate 'm_array' somehow perhaps involving multiple threads
const auto iter = std::find_if (my_array.begin (), m_array.end (), SomeLambda);
const int index = iter - my_array.begin ();
我的问题:是否存在“索引”不会索引我刚刚找到的元素(通过 concurrect_vector::operator[] 方法)的情况?我意识到另一个线程理论上可以修改元素 - 这不是我的问题。
我想这应该可行,但因为我习惯于使用原始指针,所以我想问一下(这样我就不会无意中添加细微错误的来源)。
最佳答案
concurrent_vector::iterator
是一个随机访问迭代器,因此支持并有效取差(尽管您不应该使用 int
来存储结果) .
当然,正如您所注意到的,这不是线程安全的。
关于c++ - 用 concurrent_vector 迭代器算术 : is it safe to assume that subtracting 'begin ()' from an iterator will give the index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16317076/