简体   繁体   English

线程安全和块写入大小

[英]Thread safety and block write size

Is there a minimal block size that multiple threads can write to in a contiguous block of memory that avoids race conditions or the losing of values? 是否有一个最小的块大小,多个线程可以在连续的内存块中写入,以避免竞争条件或丢失值?

For example, can 32 threads write to individual elements of this array without affecting the other values? 例如,32个线程可以写入该数组的各个元素而不影响其他值吗?

int array[32];

How about this? 这个怎么样?

bool array[32];

How about an object that stores simple true/false into a bit array? 将简单的true / false存储到位数组中的对象怎么样?

I'm guessing there is some block write size or cache related functionality that would come into play that determines this. 我猜测会有一些块写入大小或缓存相关的功能可以决定这一点。 Is that correct? 那是对的吗? And is there anything standard/safe with regards to this size(platform defines, etc)? 关于这个尺寸(平台定义等),有什么标准/安全的吗?

Is there a minimal block size that multiple threads can write to in a contiguous block of memory that avoids race conditions or the losing of values? 是否有一个最小的块大小,多个线程可以在连续的内存块中写入,以避免竞争条件或丢失值?

No. A conflict (and potential data race) only occurs if two threads access the same memory location (that is, the same byte). 不会。只有两个线程访问相同的内存位置(即相同的字节)时,才会发生冲突(和潜在的数据竞争)。 Two threads accessing different objects won't conflict. 访问不同对象的两个线程不会冲突。

For example, can 32 threads write to individual elements of this array without affecting the other values? 例如,32个线程可以写入该数组的各个元素而不影响其他值吗?

Yes, each element has its own memory location, so two threads accessing different elements won't conflict. 是的,每个元素都有自己的内存位置,因此访问不同元素的两个线程不会发生冲突。

How about this? 这个怎么样?

Yes; 是; again, each bool has its own location. 再次,每个bool都有自己的位置。

How about an object that stores simple true/false into a bit array? 将简单的true / false存储到位数组中的对象怎么样?

No; 没有; that would pack multiple values into a larger element, and two threads accessing the same larger element would conflict. 将多个值打包到一个更大的元素中,两个访问相同更大元素的线程会发生冲突。

I'm guessing there is some block write size or cache related functionality that would come into play that determines this. 我猜测会有一些块写入大小或缓存相关的功能可以决定这一点。

There could be a performance impact (known as false sharing ) when multiple threads access the same cache line; 当多个线程访问同一个缓存行时,可能会对性能产生影响(称为错误共享 ); but the language guarantees that it won't affect the program's correctness as long as they don't access the same memory location. 但是语言保证只要它们不访问相同的内存位置就不会影响程序的正确性。

There is no garuntee in standard. 标准中没有公积金。 If you need exclusive access to element you may use std::atomic .ie You may use like: 如果您需要对元素的独占访问,您可以使用std::atomic .ie您可以使用如下:

std::vector<std::atomic<int> > array;

Otherwise you are always free to use std::mutex . 否则,您可以随意使用std::mutex

can 32 threads write to individual elements of this array without affecting the other values? 32个线程可以写入该数组的各个元素而不影响其他值吗?

You are free to do this provided that one thread does interfare with other. 如果一个线程与其他线程发生冲突,您可以自由地执行此操作。 ie thread i modifies the value of array[i] ONLY. thread i修改array[i]的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM