在C ++中我做了一个函数调用4个threads.in函数有一个int值我怎么能在另一个函数中使用它 [英] In C++ I made a funcion call as 4 threads.in that funcion there is an int value how can I use it in another function
本文介绍了在C ++中我做了一个函数调用4个threads.in函数有一个int值我怎么能在另一个函数中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cv::VideoCapture a;
cv::VideoCapture b;
cv::VideoCapture c;
cv::VideoCapture d;
a.open("traffic1.mp4");
b.open("traffic2.mp4");
c.open("traffic3.mp4");
d.open("traffic4.mp4");
std::thread t1 (traffic,a,"a");
std::thread t2 (traffic, b, "b");
std::thread t3(traffic, c, "c");
std::thread t4(traffic, d, "d");
t1.join();
t2.join();
t3.join();
t4.join();
这里的函数流量()有一个整数值我怎样才能访问它什么是语法
我尝试了什么:
i没有得到任何语法的
here in function traffic() there is a integer value how can i access it what is the syntax
What I have tried:
i did't get any syntax for this
推荐答案
要在其他函数中使用,必须声明一个全局变量。你可以使用普通的int 外部交通功能。
但我认为这是一个主要的缺陷,你有不同的线程访问相同的变量如果变量不是所有线程的常量。
不清楚哪个线程赢得了访问竞争。所以最好用一些同步代码编写一个getInteger()和setInteger()函数。
请阅读文章 C ++ 11线程,锁和条件变量,以便更好地理解陷阱和解决方案。
For use in other functions you must declare a global variable. You can use a normal int outside the traffic function.
But I would call it a major flaw that you have different threads accessing the same variable if the variable isnt constant for all threads .
It is not clear which thread wins the race for access. So it is better to write an getInteger() and setInteger() function with some syncronization code.
Please read the article C++11 threads, locks and condition variables for better understanding the pitfalls and solutions.
这篇关于在C ++中我做了一个函数调用4个threads.in函数有一个int值我怎么能在另一个函数中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文