I looked up how I can pass thread arguments by reference and I found std::ref
so I used that:
void Watermarker::markDirectory(const std::string& targetDirectory, const std::string& watermarkPath)
{
std::vector<std::thread*> threads;
cv::Mat watermarkImage = loadImage(watermarkPath);
if (watermarkImage.data == NULL) { return; }
// Loop through all the files in the target directory
for (const std::filesystem::path& path : std::filesystem::directory_iterator(targetDirectory))
{
std::thread* thread = new std::thread(mark, std::ref(targetDirectory), std::ref(path), std::ref(watermarkImage));
threads.push_back(thread);
}
for (std::thread* thread : threads)
{
thread->join();
delete thread;
}
}
This is the function:
void Watermarker::mark(const std::string& targetDirectory, const std::filesystem::path& imagePath, cv::Mat& watermarkImage)
{
cv::Mat image = loadImage(imagePath.string());
if (image.data == NULL) { return; }
cv::resize(watermarkImage, watermarkImage, image.size());
// Give the images 4 channels
cv::cvtColor(image, image, cv::COLOR_RGB2RGBA);
cv::cvtColor(watermarkImage, watermarkImage, cv::COLOR_RGB2RGBA);
// Add the watermark to the original image
cv::addWeighted(watermarkImage, 0.3, image, 1, 0.0, image);
saveImage(targetDirectory + "/watermarked_images/" + imagePath.filename().string(), image);
}
But when I run this it throws an exception "Access violation writing location. I get these 2 popups:
Without the std::ref
and & in the function it works fine if I pass it by value, but I want it by reference.
question from:
https://stackoverflow.com/questions/66054774/pass-thread-arguments-by-reference-gives-me-access-violation 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…