我正在研究多线程中值函数,作为更大项目的一部分.我有很少的C++经验.下面的中值函数应该采用3维int向量的向量,并返回int的3维向量,其中每个条目是输入向量中该索引中所有条目的中值.因此,如果输入为<< 3,2,1>,<1,2,3>,<2,2,2 >>,则返回<2,2,2>.此代码将用于实现中值模糊以用于实时视频,因此需要多线程化.
#include <thread>
#include <iostream>
#include <mutex>
#include <vector>
#include <algorithm>
#include "median.h"
// mutex to protect bgrPixel (possibly not needed)
std::mutex mtx;
std::vector<int> median(const std::vector<std::vector<int> >& input)
{
std::vector<int> bgrPixel; // Vector to store median BGR value
std::thread first(thread_function, bgrPixel, input, 0); // thread for each colour channel
std::thread second(thread_function, bgrPixel, input, 1);
std::thread third(thread_function, bgrPixel, input, 2);
first.join();
second.join();
third.join();
return bgrPixel;
}
void thread_function(std::vector<int>& bgrPixel, const std::vector<std::vector<int> >& input1, int channel)
{
std::vector<int> input = …Run Code Online (Sandbox Code Playgroud)