同时写入vector< bool> [英] Write concurrently vector<bool>

查看:86
本文介绍了同时写入vector< bool>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以从std::vector同时读取而不会造成不良"后果,因为可以认为此操作是线程安全的.

I know that is possible to read concurrently from a std::vector without "bad" consequences because this operation can be considered thread-safe.

但是写操作不能说相同.但是,我想知道这是否总是不正确,例如考虑到我的特定情况.

But the same cannot be said for writing operations. But, I am wondering if this is not always true, for example considering my particular scenario.

我有一个std::vector<bool>,其中所有元素都初始化为false,并且给定一个索引数组,我需要从false更改这些元素的值(每个索引为vector[index])到true.

I have a std::vector<bool>, where all the elements are initialized to false, and, given an array of indices, I need to change the value of these elements (vector[index] for each index) from false to true.

如果我为每个索引使用不同的线程(并且某些索引可能具有相同的值),那么此操作是否可以视为线程安全的?

If I use a different thread for each index (and there is the possibility that some indices have the same value), can this operation be considered thread-safe?

如果向量是std::vector<int>(或任何原始类型),并且分配的值始终相同(例如1),是否仍可以将此操作视为线程安全的?

If the vector is a std::vector<int> (or any primitive type) and the value assigned is always the same (for example 1) can this operation still be considered thread-safe?

推荐答案

vector<bool>的并发写入永远是不可能的,因为基础实现依赖于类型为vector<bool>::reference的代理对象,该代理对象的行为就像是对c7的引用一样. bool,但实际上会根据需要获取并更新位字段字节.

Concurrent writes to vector<bool> are never ok, because the underlying implementation relies on a proxy object of type vector<bool>::reference which acts as if it was a reference to bool, but in reality will fetch and update the bitfield bytes as needed.

在不同步的情况下使用多个线程时,可能会发生以下情况:线程1应该更新一个位,并读取包含它的字节.然后线程2读取相同的字节,然后线程1更新一个位并将其写回,然后线程2更新另一个位并将该字节写回,从而覆盖了线程1的编辑.

When using multiple threads without synchronization, the following might happen: Thread 1 is supposed to update a bit, and reads the byte containing it. Then thread 2 reads the same byte, then thread 1 updates a bit and writes the byte back, and then thread 2 updates another bit and writes the byte back, overwriting the edit of thread 1.

这只是一种可能的情况,还有其他情况可能导致相同类型的数据损坏.

This is just one possible scenario, there are others that would lead to the same kind of data corruption.

vector<int>情况下,如果绝对确保所有线程将相同的值写入向量,则此操作通常不会导致数据损坏.但是,该标准当然总是要格外小心,并且将对内存位置的所有并发访问定义为未定义的行为,其中至少一个是写访问,这是未定义的行为:

In the vector<int> situation, if you are absolutely sure that all threads write the same value into the vector, then this operation will generally not lead to data corruption. However, the standard is of course always extra careful and defines all concurrent accesses to a memory location, of which at least one is a write access, to be undefined behavior:

如果两个表达式求值之一修改一个内存位置,而另一个表达式读取或修改同一个内存位置,则这两个表达式求值冲突. – intro.races/2

Two expression evaluations conflict if one of them modifies a memory location and the other one reads or modifies the same memory location. – intro.races/2

因此,一旦您对来自两个不同线程的同一元素进行任何修改操作,您将具有竞争条件并需要适当的同步,例如通过使用std::atomic<int>.

Therefore, as soon as you do any modification operation on the same element from two different threads, you will have a race condition and need proper synchronization, e.g. by using std::atomic<int>.

这篇关于同时写入vector&lt; bool&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆