我正在尝试并行化数组计算,为此我构建了一个具有 Worker(内部)类的 Shader(外部)类。外部类接收一个数组及其大小,然后继续将其分配给新的 Workers 实例。 它创建一个 worker vector ,然后是一个线程 vector ,这些线程被分配了一个 worker 函数。 不幸的是,该应用程序崩溃了。故障并使用调试器 我已经确认一些对象没有被实例化(而有些实际上是)并且缺少所需的数据。 由于实例化不是并行完成的,因此不应存在任何类型的竞争条件。
我正在使用 mingw_64 作为编译器。其他图书馆是 <iostream>,<c++/4.8.3/sstream>,<math.h>,<SDL.h>,<SDL_image.h>,<thread>,<vector>
.
外部类的主要功能:
void BlurShader::render()
{
int threadsNum;
for (threadsNum = 6; threadsNum > 1; threadsNum--)
{
if (height % threadsNum == 0) break;
}
int heightStep = height / threadsNum;
auto *workerObjects = new vector<Worker*>;
auto *workerThreads = new vector<thread *>;
// Instantiating workers:
for (int i = 0; i < threadsNum; i++)
{
workerObjects->push_back(
new Worker(this->original, this->palette, this->width, this->height,
i * heightStep,((i + 1) * heightStep) - 1));
}
/* As you can see a lot of information is streamed into the worker,
and it is relying on it. Then in a second for loop I create
threads: */
for (int i = 0; i < threadsNum; i++)
{
workerThreads->push_back(new thread([&]()
{
(*workerObjects)[i]->threadProcess();
}));
}
// Then the main thread waits for threads to finish:
for (int j = 0; j < threadsNum; j++)
{
(*workerThreads)[j]->join();
delete (*workerThreads)[j];
}
// Cleanup
for(int i=0;i<threadsNum;i++)
{
delete (*workerObjects)[i];
}
delete workerObjects;
delete workerThreads;
memcpy(original, palette, height * width * size);
}
期待您的建议。 如果您发现我错误地使用线程,我会很乐意倾听。我只学习了一个星期的 C++,所以一切顺利。
最佳答案
问题在于 lambda 捕获 i
的方式:
for (int i = 0; i < threadsNum; i++)
{
workerThreads->push_back(new thread([&]() // [1]
{
(*workerObjects)[i]->threadProcess();
}));
}
i
是通过引用捕获的,但是您不知道什么时候线程体即闭包被调用,所以可能 i
被修改了(由 for 循环)在 i
的正确值被读取之前。
通过复制传递i
:
for (int i = 0; i < threadsNum; i++)
{
workerThreads->push_back(new thread([&,i]() // pass i by copy
{
(*workerObjects)[i]->threadProcess();
}));
}
关于c++ - 具有成员函数的实例化对象,在将其成员函数传递给新线程时表现为未实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56969436/