Indeed, if you access your a
and b
variables from several threads, there will be a problem of mutual exclusion. This can be solved by using a mutex
. You need to lock
the mutex before read and/or write the variables, and unlock
it after. This can be done by lock_guard
.
You can do something like that :
#include <mutex>
#include <thread>
void MyThreadFunction(mutex& m, cv::Mat& a, cv::Mat& b)
{
while (1)
{
[ ... ]
{
lock_gard<mutex> l(m);
capture_L.read(a);
capture_R.read(b);
}
}
}
int main()
{
mutex m;
cv::Mat a, b;
thread t(MyTjreadFunction, ref(m), ref(a), ref(b));
{
lock_gard<mutex> l(m);
[ ... access a & b ... ]
}
t.join();
return 0;
}