Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将现有函数放入自己的连续(循环)线程中,并将更新的参数传递给它?_C++_Multithreading_Opencv_Parameter Passing - Fatal编程技术网

C++ 如何将现有函数放入自己的连续(循环)线程中,并将更新的参数传递给它?

C++ 如何将现有函数放入自己的连续(循环)线程中,并将更新的参数传递给它?,c++,multithreading,opencv,parameter-passing,C++,Multithreading,Opencv,Parameter Passing,我有一个OpenCV控制台应用程序,它从一个端口接收相机帧并将其显示在屏幕上,首先对其执行可选的图像处理例程。main()循环是连续的,即使用while(true),在每次传递时,它都会得到一个新的图像,等待它放入垫子中。我需要在main()中保持至少30fps的速率,以便传入帧不会被丢弃 通常这不是一个问题,除非我有密集的处理,但当我这样做时,我想至少卸载一些简单的例程到他们自己的线程,这样他们就不会占用线性CPU时间。它们可以通过“抓取”任何帧并对其执行操作来独立运行,并将结果异步显示到ma

我有一个OpenCV控制台应用程序,它从一个端口接收相机帧并将其显示在屏幕上,首先对其执行可选的图像处理例程。main()循环是连续的,即使用while(true),在每次传递时,它都会得到一个新的图像,等待它放入垫子中。我需要在main()中保持至少30fps的速率,以便传入帧不会被丢弃

通常这不是一个问题,除非我有密集的处理,但当我这样做时,我想至少卸载一些简单的例程到他们自己的线程,这样他们就不会占用线性CPU时间。它们可以通过“抓取”任何帧并对其执行操作来独立运行,并将结果异步显示到main()。例如,直方图例程和计算全局对比度/偏移调整值的例程

我看到了一个使用语法的简单示例,其中3个例程在main()中各自的线程中启动,它们独立运行,然后在main()的末尾重新连接,然后停止执行。下面就是这个例子,我将方法1合并到我的应用程序中,因为它看起来最简单。(我不知道lambda是什么)

//CPP程序使用三种不同的可调用函数演示多线程。
#包括
#包括
使用名称空间std;
//伪函数
void foo(int Z)
{
对于(int i=0;icout我不确定我是否已经很好地理解了您想要做什么,但是我猜您想要启动线程,其参数值在每次“迭代”时都会更新,对吗

如果是这样,您可以在
main()
函数中声明参数,并通过引用线程来提供这些参数。
main()
函数将执行更新,这样,线程将考虑这些参数

类似的内容:

//...
void foo (int &a, bool &stop) // pass by reference
{
    while(!stop)
    {
        // Do something with a
    }
}
void bar(char &c, bool &b, bool &stop) // pass by reference too
{
    while(!stop)
    {
        // Do something with c and b
    }
}
//...
int main()
{
    std::atomic <bool> stop_threads(false);

    // declare your parameters (and initialize them the way you want)
    int param1(0);
    char param2('a');
    bool param3(false);

    // launch your threads
    thread th1(foo, std::ref(param1), std::ref(stop_threads));
    thread th2(bar, std::ref(param2), std::ref(param3), std::ref(stop_threads));

    // Update the parameters in your while loop
    while(/* Your stop condition */)
    {
        // Update the value of param1, param2 and param3
        // ...
    }

    // When exiting the while loop, the process is ended (no more data to process)
    stop_threads = true;

    th1.join();
    th2.join();

    return 0;
}
/。。。
void foo(int&a、bool&stop)//按引用传递
{
当(!停止)
{
//带着一个微笑做某事
}
}
无效条(字符和c、布尔和b、布尔和停止)//也通过引用传递
{
当(!停止)
{
//用c和b做点什么
}
}
//...
int main()
{
std::原子停止线程(false);
//声明您的参数(并以您想要的方式初始化它们)
int参数1(0);
字符参数2('a');
布尔参数3(假);
//启动你的线程
线程th1(foo,std::ref(param1),std::ref(stop_线程));
螺纹th2(条形、标准::参考(参数2)、标准::参考(参数3)、标准::参考(停止螺纹));
//更新while循环中的参数
而(/*您的停止条件*/)
{
//更新param1、param2和param3的值
// ...
}
//退出while循环时,进程结束(不再处理数据)
停止线程=真;
th1.join();
th2.join();
返回0;
}
我在要在不同线程中启动的每个函数的参数中添加了一个额外的
boolean
,以便让它们在结束时正确停止

我希望这就是你想要的:)
如果我误解了你的问题,请不要犹豫告诉我


编辑:

//...
void foo (int &a, bool &stop) // pass by reference
{
    while(!stop)
    {
        // Do something with a
    }
}
void bar(char &c, bool &b, bool &stop) // pass by reference too
{
    while(!stop)
    {
        // Do something with c and b
    }
}
//...
int main()
{
    std::atomic <bool> stop_threads(false);

    // declare your parameters (and initialize them the way you want)
    int param1(0);
    char param2('a');
    bool param3(false);

    // launch your threads
    thread th1(foo, std::ref(param1), std::ref(stop_threads));
    thread th2(bar, std::ref(param2), std::ref(param3), std::ref(stop_threads));

    // Update the parameters in your while loop
    while(/* Your stop condition */)
    {
        // Update the value of param1, param2 and param3
        // ...
    }

    // When exiting the while loop, the process is ended (no more data to process)
    stop_threads = true;

    th1.join();
    th2.join();

    return 0;
}
我忘了提到一件非常重要的事情。每次我写“使用该变量做点什么…”或“更新该变量的值”,都需要在共享变量上使用互斥机制(互斥),以避免不同线程同时访问同一个变量。
我已经通过
std::atomic stop_threads
更改了
bool stop_threads
,以避免所有线程共享stop标志的争用情况。

我不确定我是否已经很好地理解了您想要做的事情,但我猜您希望启动每个“迭代”都会更新参数值的线程,对吗

如果是这样,您可以在
main()
函数中声明参数,并通过引用线程来提供这些参数。
main()
函数将执行更新,这样,线程将考虑这些参数

类似的内容:

//...
void foo (int &a, bool &stop) // pass by reference
{
    while(!stop)
    {
        // Do something with a
    }
}
void bar(char &c, bool &b, bool &stop) // pass by reference too
{
    while(!stop)
    {
        // Do something with c and b
    }
}
//...
int main()
{
    std::atomic <bool> stop_threads(false);

    // declare your parameters (and initialize them the way you want)
    int param1(0);
    char param2('a');
    bool param3(false);

    // launch your threads
    thread th1(foo, std::ref(param1), std::ref(stop_threads));
    thread th2(bar, std::ref(param2), std::ref(param3), std::ref(stop_threads));

    // Update the parameters in your while loop
    while(/* Your stop condition */)
    {
        // Update the value of param1, param2 and param3
        // ...
    }

    // When exiting the while loop, the process is ended (no more data to process)
    stop_threads = true;

    th1.join();
    th2.join();

    return 0;
}
/。。。
void foo(int&a、bool&stop)//按引用传递
{
当(!停止)
{
//带着一个微笑做某事
}
}
无效条(字符和c、布尔和b、布尔和停止)//也通过引用传递
{
当(!停止)
{
//用c和b做点什么
}
}
//...
int main()
{
std::原子停止线程(false);
//声明您的参数(并以您想要的方式初始化它们)
int参数1(0);
字符参数2('a');
布尔参数3(假);
//启动你的线程
线程th1(foo,std::ref(param1),std::ref(stop_线程));
螺纹th2(条形、标准::参考(参数2)、标准::参考(参数3)、标准::参考(停止螺纹));
//更新while循环中的参数
而(/*您的停止条件*/)
{
//更新param1、param2和param3的值
// ...
}
//退出while循环时,进程结束(不再处理数据)
停止线程=真;
th1.join();
th2.join();
返回0;
}
我在要在不同线程中启动的每个函数的参数中添加了一个额外的
boolean
,以便让它们在结束时正确停止

我希望这就是你想要的:)
如果我误解了你的问题,请不要犹豫告诉我


编辑:

//...
void foo (int &a, bool &stop) // pass by reference
{
    while(!stop)
    {
        // Do something with a
    }
}
void bar(char &c, bool &b, bool &stop) // pass by reference too
{
    while(!stop)
    {
        // Do something with c and b
    }
}
//...
int main()
{
    std::atomic <bool> stop_threads(false);

    // declare your parameters (and initialize them the way you want)
    int param1(0);
    char param2('a');
    bool param3(false);

    // launch your threads
    thread th1(foo, std::ref(param1), std::ref(stop_threads));
    thread th2(bar, std::ref(param2), std::ref(param3), std::ref(stop_threads));

    // Update the parameters in your while loop
    while(/* Your stop condition */)
    {
        // Update the value of param1, param2 and param3
        // ...
    }

    // When exiting the while loop, the process is ended (no more data to process)
    stop_threads = true;

    th1.join();
    th2.join();

    return 0;
}
我忘了提到一件非常重要的事情。每次我写“使用该变量做点什么…”或“更新该变量的值”,都需要在共享变量上使用互斥机制(互斥),以避免不同线程同时访问同一个变量。
我已经通过
std::atomic stop_threads
更改了
bool stop_threads
,以避免所有线程共享stop标志的争用情况。

我认为在您的示例中,对于线程到底在做什么以及您想要做什么有些混淆<
Legend: - queued for execution, * Executing, R read, W write
MAIN: ****W*****W*****W-------------------***R***R***R***
T1  : -----------------***R**----------------------------
T2  : -----------------------**R***R**W------------------
T3  :----------------------------------------------------