由 std::shared_ptr 向量指向的数据之间的线程同步

Thread synchronization between data pointed by vectors of std::shared_ptr

本文关键字:数据 之间 线程 同步 向量 shared std ptr      更新时间:2023-10-16

我对并发编程很陌生,我有一个特定的问题,我无法通过浏览互联网找到解决方案。

基本上我有这种情况(原理图伪代码):

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。 这是否有效/是否必要将取决于在函数fun1fun2中实际执行的操作以及如何管理这些互斥体。

如果有必要在任何一个循环开始之前锁定,那么并行实际上可能没有任何好处,因为fun1()fun2()中的一个基本上会等待另一个完成,并且两者实际上将串联运行。