[英]What is the memory footprint of std::atomic in C++11, in practice?
A program that I am writing needs to store significant amounts of data (several gigabytes) in ram to be accessed atomically by many threads. 我正在编写的程序需要在ram中存储大量数据(几千兆字节),以便由多个线程以原子方式访问。
std::atomic
seems to be a reasonable way to do this, because its accesses are likely to be more efficient than wrapping all accesses in one or more std::mutex
s, since, worst case, it will use a mutex internally and be equivalent. std::atomic
似乎是一种合理的方法,因为它的访问可能比在一个或多个std::mutex
包装所有访问更有效,因为,最坏的情况是,它将在内部使用互斥量并且是当量。
My data is organized as a set of Chunk
objects, which have, among others, an array member containing most of their data. 我的数据被组织为一组
Chunk
对象,其中包含一个包含大部分数据的数组成员。 Right now, I am thinking about defining it as std::array<std::atomic<unsigned int>, SOME_CONSTANT_HERE>
, but this is only going to be efficient if the memory footprint of std::atomic
on built-in types such as unsigned int
is no worse than unsigned int
itself, for, based on my calculations, with the amount of data I need to store, current common ram capacities are barely sufficient. 现在,我正在考虑将其定义为
std::array<std::atomic<unsigned int>, SOME_CONSTANT_HERE>
,但这只有在内置类型的std::atomic
的内存占用时才会有效。因为unsigned int
本身并不比unsigned int
本身差,因为根据我的计算,我需要存储的数据量,当前常见的ram容量勉强够用。
The only alternative that I see (other alternatives are welcome) is to have each chunk have its own std::mutex
instance, but that is problematic because often threads will need to work with several chunks at the same time (not nearly all, however), and holding multiple locks is both problematic for deadlocks, and will cause significant contention due to the patterns of which chunks are accessed by various threads. 我看到的唯一替代方案(其他替代方案是受欢迎的)是让每个块都有自己的
std::mutex
实例,但这是有问题的,因为线程通常需要同时使用多个块(不过几乎所有块)并且持有多个锁对于死锁都是有问题的,并且由于各种线程访问块的模式将导致显着的争用。
So, what is the memory footprint in practice on x86_64 of std::atomic
for integral types? 那么,对于整数类型,
std::atomic
x86_64实际上的内存占用量是多少?
EDIT: I tried searching around on Google, and even a bit of digging in the GNU standard library source, to no avail. 编辑:我尝试在Google上搜索,甚至在GNU标准库源代码中挖掘,但无济于事。
You can check it yourself here: https://godbolt.org/g/6zjJCU - with no optimization turned on, both std::atomic
and a regular variable take the same amount of memory. 您可以在这里自己检查: https : //godbolt.org/g/6zjJCU - 没有打开优化,
std::atomic
和常规变量都占用相同的内存量。
The difference, however, is the access to the variables. 然而,不同之处在于对变量的访问。 Try to uncomment the block... you get all the static calls and data protection.
尝试取消注释块...您将获得所有静态调用和数据保护。
If you do a lot of read/writes, a mutex may be more efficient - maybe you can lock the mutex and then read, say, 100 bytes of data? 如果你进行大量的读/写操作,互斥锁可能会更有效 - 也许你可以锁定互斥锁,然后读取100字节的数据? Also, be careful for dirty reads/writes: if you read an atomic, do something with the value, and then try to update the atomic, interleaved thread may have already changed the atomic's value.
另外,请注意脏读/写:如果您读取原子,使用该值执行某些操作,然后尝试更新原子,则交错的线程可能已经更改了原子的值。 This can easily be avoided with a slightly more costly mutex.
使用稍微昂贵的互斥锁可以轻松避免这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.