in Education by
Having two thread, one product data, another one process data. The data is not just an int or float but a complex object. In my case, it's an OpenCV Mat(an image). If the first thread only created half-size of the image, and second thread read it, will get half size of the image? The image will be broken? int main(int argc, char *argv[]) { cv::Mat buffer; cv::VideoCapture cap; std::mutex mutex; cap.open(0); std::thread product([](cv::Mat& buffer, cv::VideoCapture cap, std::mutex& mutex){ while (true) { // keep product the new image cv::Mat tmp; cap >> tmp; //mutex.lock(); buffer = tmp.clone(); //mutex.unlock(); } }, std::ref(buffer), cap, std::ref(mutex)); product.detach(); int i; while (true) { // process in the main thread //mutex.lock(); cv::Mat tmp = buffer; //mutex.unlock(); if(!tmp.data) std::cout<<"null"<<i++<<std::endl; else { //std::cout<<"not null"<<std::endl; cv::imshow("test", tmp); } if(cv::waitKey(30) >= 0) break; } return 0; } Do I need to add a mutex around write and read to make sure the image not be broken? Like this: int main(int argc, char *argv[]) { cv::Mat buffer; cv::VideoCapture cap; std::mutex mutex; cap.open(0); std::thread product([](cv::Mat& buffer, cv::VideoCapture cap, std::mutex& mutex){ while (true) { // keep product the new image cv::Mat tmp; cap >> tmp; mutex.lock(); buffer = tmp.clone(); mutex.unlock(); } }, std::ref(buffer), cap, std::ref(mutex)); product.detach(); while (true) { // process in the main thread mutex.lock(); cv::Mat tmp = buffer; mutex.unlock(); if(!tmp.data) std::cout<<"null"<<std::endl; else { std::cout<<"not null"<<std::endl; cv::imshow("test", tmp); } } return 0; } This question related to How to solves image processing cause camera io delay? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
As soon as you have one thread modifying an object while another thread potentially accesses the value of that same object concurrently, you have a race condition and behavior is undefined. Yes, that can happen. And, since we're talking about an object like an entire image buffer here, almost certainly will happen. And yes, you will need to use proper synchronization to prevent it from happening. From your description, it would seem that you basically have a situation here where one thread is producing some image and another thread has to wait for the image to be ready. In this case, the first question that you should ask yourself is: if the second thread cannot start its work before the first thread has completed its work, then what exactly are you gaining by using a second thread here? If there is still enough work that both threads can do in parallel for this all to make sense, then you will most likely want to use not just a simple mutex here, but more something like, e.g., a condition variable or a barrier…

Related questions

0 votes
    Which of the following package combine multi-dimensional arrays? (a) stringr (b) comb (c) abind (d) ... Regression of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have just started to get a feel of Dicom standard. I am trying to write a small program, that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Which of the following statement read a tab or space delimited file? (a) read.table(filename,header=TRUE) ( ... of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Which of the following statement can read csv files? (a) read.table(filename,header=TRUE,sep=',') (b) ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I would like to use CHOLMOD's GPU acceleration, and have found several simple examples on how to use ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    ____________ function can be used to select the random sample of size n' from a huge dataset. (a) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which of the following code create n samples of size “size” with probability prob from the binomial? (a) z...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Which of the following sets the size of the outer margins for the graph? (a) par(mfrow=c(nrow,mcol)) ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Consider the tree T in which left subtree contains half of the maximum number of nodes possible in the avl tree of ... of nodes in T? Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    What will happen if two thread of the same priority are called to be processed simultaneously? (a) Anyone ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...