0

我有一个遵循以下结构的代码:

//some const variables here


std::function<void(int threadID, NumericMatrix * mat)> threadT = [const1,const2](int threadID, NumericMatrix * mat) {
//a lot of variables defined inside lamdas

}

NumericMatrix * mat1 = new NumericMatrix(N, nEstimators);
std::thread t1( threadT, 1, mat1 );
NumericMatrix * mat2 = new NumericMatrix(N, nEstimators);
std::thread t2( threadT, 2, mat2 );
t1.join();
t2.join();

return;

当我尝试运行它时,我会收到很多分段错误错误,但是当我尝试只运行一个线程时没有问题。我不认为有互斥锁的问题,两个线程都只访问定义的变量

所以我想知道:是不是因为我为每个线程重用了相同的 lambda?如果是这样,我该如何解决这个问题?通过制作 threadT 的副本(如何)?

Obs.:我找不到用 gdb 调试它的方法,因为它是在 R 中编译的。

4

0 回答 0