QVector项目共享相同的缓存行吗?

时间:2021-02-19 21:00:43

Let's say I have a QVector storing this elements: {3, 4, 1, 5, 6}. So if I read each element from different threads, will this cause false-sharing? (by 'read' I mean something like: int i = vector[0]; //no change involved)

假设我有一个存储这些元素的QVector:{3,4,1,5,6}。因此,如果我从不同的线程中读取每个元素,这会导致错误共享吗? (通过'读'我的意思是:int i = vector [0]; //不涉及任何变化)

And with the same concept of 'read', Is a single element of the vector considered a shared-resource when 'reading' it from different threads?

并且使用相同的“读取”概念,当从不同的线程“读取”它时,向量的单个元素是否被视为共享资源?

2 个解决方案

#1


Yes multiple elements will be available per cache line as QVector stores it's elements contiguously in memory (as does std::vector).

是的,每个缓存行可以使用多个元素,因为QVector将其元素连续存储在内存中(与std :: vector一样)。

So false sharing can occur, but on modern processors it will only cause performance degradation if the cache line is modified - which it isn't in your example.

因此可能会发生错误共享,但在现代处理器上,如果修改了缓存行,它只会导致性能下降 - 这不在您的示例中。

Is a single element of the vector considered a shared-resource when 'reading' it from different threads?

当从不同的线程“读取”向量时,向量的单个元素是否被视为共享资源?

It depends what you mean by a 'shared-resource'. If you mean a cache line marked as shared, then yes but only because the cache line contains the element, there's nothing special about the element itself.

这取决于你所谓的“共享资源”。如果你的意思是标记为共享的缓存行,那么只是因为缓存行包含元素,所以元素本身并没有什么特别之处。

If you're actually referring to Qt's implicit sharing mechanism (which QVector uses), then no a single element is not considered a shared resource as the reference counting occurs at the container level.

如果您实际上是指Qt的隐式共享机制(QVector使用),则不会将单个元素视为共享资源,因为引用计数发生在容器级别。

#2


QVector and all the source of Qt is available for examination.

QVector和Qt的所有来源都可供检查。

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.h

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.cpp

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector_msvc.cpp

The official documentation says:

官方文件说:

http://doc.qt.io/qt-5/qvector.html#details

QVector<T> is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.

QVector 是Qt的通用容器类之一。它将其项目存储在相邻的内存位置,并提供快速的基于索引的访问。

And there is a lot more description in there about using the [] operator v. using at(int index) v. using data() to access the elements of the array.

关于使用[]运算符v。使用at(int index)v。使用data()来访问数组元素,有很多描述。

Qt Thread safety is spelled out clearly here:

Qt线程安全在这里清楚地说明:

http://doc.qt.io/qt-5/threads-reentrancy.html

http://doc.qt.io/qt-5/thread-basics.html

http://doc.qt.io/qt-5/threads-synchronizing.html

And others.

#1


Yes multiple elements will be available per cache line as QVector stores it's elements contiguously in memory (as does std::vector).

是的,每个缓存行可以使用多个元素,因为QVector将其元素连续存储在内存中(与std :: vector一样)。

So false sharing can occur, but on modern processors it will only cause performance degradation if the cache line is modified - which it isn't in your example.

因此可能会发生错误共享,但在现代处理器上,如果修改了缓存行,它只会导致性能下降 - 这不在您的示例中。

Is a single element of the vector considered a shared-resource when 'reading' it from different threads?

当从不同的线程“读取”向量时,向量的单个元素是否被视为共享资源?

It depends what you mean by a 'shared-resource'. If you mean a cache line marked as shared, then yes but only because the cache line contains the element, there's nothing special about the element itself.

这取决于你所谓的“共享资源”。如果你的意思是标记为共享的缓存行,那么只是因为缓存行包含元素,所以元素本身并没有什么特别之处。

If you're actually referring to Qt's implicit sharing mechanism (which QVector uses), then no a single element is not considered a shared resource as the reference counting occurs at the container level.

如果您实际上是指Qt的隐式共享机制(QVector使用),则不会将单个元素视为共享资源,因为引用计数发生在容器级别。

#2


QVector and all the source of Qt is available for examination.

QVector和Qt的所有来源都可供检查。

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.h

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector.cpp

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qvector_msvc.cpp

The official documentation says:

官方文件说:

http://doc.qt.io/qt-5/qvector.html#details

QVector<T> is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.

QVector 是Qt的通用容器类之一。它将其项目存储在相邻的内存位置,并提供快速的基于索引的访问。

And there is a lot more description in there about using the [] operator v. using at(int index) v. using data() to access the elements of the array.

关于使用[]运算符v。使用at(int index)v。使用data()来访问数组元素,有很多描述。

Qt Thread safety is spelled out clearly here:

Qt线程安全在这里清楚地说明:

http://doc.qt.io/qt-5/threads-reentrancy.html

http://doc.qt.io/qt-5/thread-basics.html

http://doc.qt.io/qt-5/threads-synchronizing.html

And others.