由 std::shared_ptr 向量指向的数据之间的线程同步
Thread synchronization between data pointed by vectors of std::shared_ptr
我对并发编程很陌生,我有一个特定的问题,我无法通过浏览互联网找到解决方案。
基本上我有这种情况(原理图伪代码):
void fun1(std::vector<std::shared_ptr<SmallObj>>& v) {
for(int i=0; i<v.size(); i++)
.. read and write on *v[i] ..
}
void fun2(std::vector<std::shared_ptr<SmallObj>>& w) {
for(int i=0; i<w.size(); i++)
.. just read on *w[i] ..
}
int main() {
std::vector<std::shared_ptr<SmallObj>> tot;
for(int iter=0; iter<iterMax; iter++) {
for(int nObj=0; nObj<nObjMax; nObj++)
.. create a SmallObj in the heap and store a shared_ptr in tot ..
std::vector<std::shared_ptr<SmallObj>> v, w;
.. copy elements of "tot" in v and w ..
fun1(v);
fun2(w);
}
return 0;
}
我想做的是同时生成两个线程来执行 fun1 和 fun2,但我需要使用一些锁定机制来调节对 SmallObjs 的访问。我该怎么做?在文献中,我只能找到使用互斥锁锁定对特定对象或部分代码的访问的示例,但不能找到不同对象(在本例中为 v 和 w)对相同指向变量的访问
。非常感谢你,很抱歉我对这件事的无知。
我需要使用一些锁定机制来调节对 SmallObjs 的访问。我该怎么做?
对数据成员使用getter和setter。使用std::mutex
(或std::recursive_mutex
,取决于是否需要递归锁定)数据成员来保护访问,然后始终使用锁保护进行锁定。
示例(另请参阅代码中的注释):
class SmallObject{
int getID() const{
std::lock_guard<std::mutex> lck(m_mutex);
return ....;
}
void setID(int id){
std::lock_guard<std::mutex> lck(m_mutex);
....;
}
MyType calculate() const{
std::lock_guard<std::mutex> lck(m_mutex);
//HERE is a GOTCHA if `m_mutex` is a `std::mutex`
int k = this->getID(); //Ooopsie... Deadlock
//To do the above, change the decaration of `m_mutex` from
//std::mutex, to std::recursive_mutex
}
private:
..some data
mutable std::mutex m_mutex;
};
最简单的解决方案是保持整个向量的std::mutex
:
#include <mutex>
#include <thread>
#include <vector>
void fun1(std::vector<std::shared_ptr<SmallObj>>& v,std::mutex &mtx) {
for(int i=0; i<v.size(); i++)
//Anything you can do before read/write of *v[i]...
{
std::lock_guard<std::mutex> guard(mtx);
//read-write *v[i]
}
//Anything you can do after read/write of *v[i]...
}
void fun2(std::vector<std::shared_ptr<SmallObj>>& w,std::mutex &mtx) {
for(int i=0; i<w.size(); i++) {
//Anything that can happen before reading *w[i]
{
std::lock_guard<std::mutex> guard(mtx);
//read *w[i]
}
//Anything that can happen after reading *w[i]
}
int main() {
std::mutex mtx;
std::vector<std::shared_ptr<SmallObj>> tot;
for(int iter=0; iter<iterMax; iter++) {
for(int nObj=0; nObj<nObjMax; nObj++)
.. create a SmallObj in the heap and store a shared_ptr in tot ..
std::vector<std::shared_ptr<SmallObj>> v, w;
.. copy elements of "tot" in v and w ..
std::thread t1([&v,&mtx] { fun1(v,mtx); });
std::thread t2([&w,&mtx] { fun2(w,mtx); });
t1.join();
t2.join();
}
return 0;
}
但是,您实际上只会在fun1()
和fun2()
循环中的前/后块中完成的位上获得任何并行性。
您可以通过引入更多锁来进一步提高并行性。 例如,您可能只能使用2个控制奇数和偶数元素的互斥锁:
void fun1(std::vector<int>&v,std::mutex& mtx0,std::mutex& mtx1 ){
for(size_t i{0};i<v.size();++i){
{
std::lock_guard<std::mutex> guard(i%2==0?mtx0:mtx1);
//read-write *v[i]
}
}
}
具有类似的fun2()
格式.
您可以通过从向量的两端工作或使用try_lock
并移动到后续元素并在可用时"返回"锁定元素来减少争用。
如果一个函数的迭代执行比另一个函数的执行要大得多,并且在另一个函数完成之前从"更快"的函数获得结果有一些优势,那么这一点可能是最重要的。
选择:
显然,可以为每个对象添加std::mutex
。 这是否有效/是否必要将取决于在函数fun1
和fun2
中实际执行的操作以及如何管理这些互斥体。
如果有必要在任何一个循环开始之前锁定,那么并行实际上可能没有任何好处,因为fun1()
或fun2()
中的一个基本上会等待另一个完成,并且两者实际上将串联运行。
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 线程之间的实时数据共享
- std::weak_ptr 和相应的 std::shared_ptr 之间是否存在数据竞争?
- 在C++事务之间存储大量字符数据的有效方法
- 如何在进程之间共享大量数据而不重复?(国际刑罚委员会)
- 简单使用 std::atomic 在两个线程之间共享数据
- 在 EXE/DLL 之间传递数据
- 由 std::shared_ptr 向量指向的数据之间的线程同步
- 在压缩和未压缩数据之间切换
- 尝试从每个预期数据之间有多个空间的文件中读取和存储数据
- 如何在指定数据之间将数据从一个向量获取到另一个向量
- 使用Isringstream和struct/vector在给定的数据之间建立连接
- 结构数据D = {0}和结构数据D = {}之间是否存在任何区别
- openMP 的目标数据和目标数据之间的区别?
- 代码是否被视为可移植可执行文件格式中的初始化数据,初始化数据和单元化数据之间的确切区别是什么
- libpcap 数据包结果与使用 C++ 的 BSD 套接字接收数据之间的差异