C++ std::threads 交互,尽管它们不应该
C++ std::threads interacting although they should not
让我提前对这个问题含糊不清表示歉意。我不能分享任何细节。这是一个计算机视觉问题,在 main() 中扫描并处理了一堆图像。每 100(左右)帧 main() 函数生成另一个线程,以不同的方式扫描图像(但第一步是必需的)。
#include <thread>
...
void myFunction(ImageVector, arg2, arg3)
{
for (i = 0; i < ImageVector.size < ++i)
{
process(ImageVector[i])
}
}
int main(void)
{
std::vector<std::string> images; // paths to the images, please consider it filled somehow, e.g. multiple push_backs
for (i = 0; i < Images.size ; ++i)
{
Scan(Images[i]). // Scans the image
Process(Images[i]) // Processes the image
if (Something)
{
std::vector<std::string> ImageVector(&Images[10], &Images[100]);
//Spawn the tread and put it into a vector.
MyThreads.push_back(std::thread(myFunction, ImageVector, arg2, arg3));
}
}
MyThreads[0].join(); // assuming only the first one is filled
}
图片不一样,是从磁盘读取的。不过我检查了阅读逻辑,这不是问题所在。如果我一个接一个地执行任务,它工作正常。 ImageVector、arg2、arg3 都不是指针。它们只是变量的副本。
函数 myFunction()、Scan()、Process() 使用相似的代码片段,但这些片段中的 none 具有共享变量。也没有 malloc(),因为有人告诉我这不是线程安全的,现在只有 "new"。
我意识到继续下去并不多,但也许我只是忘记了一些简单的事情。
谢谢
PS: 我把线程库切换到 boost 没有效果,'。
如果一个程序在没有线程的情况下工作并且不使用线程,那么可能的原因可能是同步。在这种特定情况下,由于问题描述和可用代码片段有限,访问 ImageVector 时可能会出现同步问题。
让我提前对这个问题含糊不清表示歉意。我不能分享任何细节。这是一个计算机视觉问题,在 main() 中扫描并处理了一堆图像。每 100(左右)帧 main() 函数生成另一个线程,以不同的方式扫描图像(但第一步是必需的)。
#include <thread>
...
void myFunction(ImageVector, arg2, arg3)
{
for (i = 0; i < ImageVector.size < ++i)
{
process(ImageVector[i])
}
}
int main(void)
{
std::vector<std::string> images; // paths to the images, please consider it filled somehow, e.g. multiple push_backs
for (i = 0; i < Images.size ; ++i)
{
Scan(Images[i]). // Scans the image
Process(Images[i]) // Processes the image
if (Something)
{
std::vector<std::string> ImageVector(&Images[10], &Images[100]);
//Spawn the tread and put it into a vector.
MyThreads.push_back(std::thread(myFunction, ImageVector, arg2, arg3));
}
}
MyThreads[0].join(); // assuming only the first one is filled
}
图片不一样,是从磁盘读取的。不过我检查了阅读逻辑,这不是问题所在。如果我一个接一个地执行任务,它工作正常。 ImageVector、arg2、arg3 都不是指针。它们只是变量的副本。
函数 myFunction()、Scan()、Process() 使用相似的代码片段,但这些片段中的 none 具有共享变量。也没有 malloc(),因为有人告诉我这不是线程安全的,现在只有 "new"。
我意识到继续下去并不多,但也许我只是忘记了一些简单的事情。
谢谢
PS: 我把线程库切换到 boost 没有效果,'。
如果一个程序在没有线程的情况下工作并且不使用线程,那么可能的原因可能是同步。在这种特定情况下,由于问题描述和可用代码片段有限,访问 ImageVector 时可能会出现同步问题。