[英]Getting segfault using c++11 std::thread
static T MultiplyElement(const Matrix& matrixA, const Matrix& matrixB,
unsigned M2col, unsigned M1row)
{
T sumToReturn = 0;
for (unsigned iM1colM2row = 0; iM1colM2row < matrixA.m_n; iM1colM2row++)
{
sumToReturn +=
matrixA.m_data[M1row][iM1colM2row] * matrixB.m_data[iM1colM2row][M2col];
}
return sumToReturn;
}
... ...
std::vector<std::thread> threads;
for(unsigned i = 0; i < outM ; ++i)
{
for(unsigned j = 0; j < outN; ++j)
{
threads.push_back(std::thread([&]()
{
outMatrix.m_data[i][j] = MultiplyElement(matrixA, matrixB, i, j);
}
));
}
}
for(auto& thread : threads)
{
thread.join();
}
Compiled with: clang++ -std=c++11 -stdlib=libc++ newFile.cpp 编译:clang ++ -std = c ++ 11 -stdlib = libc ++ newFile.cpp
I'm getting a segfault in MultiplyElement... any idea why? 我在MultiplyElement中遇到段错误...为什么知道?
I think the problem is with your capture. 我认为问题出在您的捕获上。 You are using reference-capture for all variables.
您正在对所有变量使用引用捕获。 You should be capturing
i
and j
by value. 您应该按值捕获
i
和j
。 Try [&, i, j]
for your capture clause. 尝试使用
[&, i, j]
作为您的捕获子句。
Edit: You can check the answer here . 编辑:您可以在此处检查答案 。 You have the same issue.
你有同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.