#include <thread>
#include <iostream>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include <mutex>
using namespace std;
using namespace cv;
std::mutex mtx;
bool flag = false;
cv::Mat image = imread("/home/cver/图片/1.png");
cv::VideoCapture capture(0);
//cv::Mat test;
void fun1(int a)
{
while(1)
{
mtx.lock();
//test = image;
cv::imshow("thread1",image);
cv::waitKey(1);
//usleep(100);
mtx.unlock();
}
}
void fun2(int b)
{
while(1)
{
mtx.lock();
cv::imshow("thread2",image);
cv::waitKey(1);
//usleep(100);
mtx.unlock();
}
flag = true;
}
int main()
{
//int a =3;
std::thread temp1(fun1,3);
temp1.detach();
std::thread temp2(fun2,3);
temp2.detach();
while(1)
{
capture >> image;
if(flag)
break;
}
cout << "multi thread test finish!" << endl;
return 0;
}
CMakelists.txt
cmake_minimum_required(VERSION 2.8)
project(simpleThread)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "-pthread")
find_package(OpenCV REQUIRED)
add_executable(${PROJECT_NAME} "simpleMultiThread.cpp"
)
target_link_libraries(simpleThread ${OpenCV_LIBS})