I have a concurrent_vector (ppl) container declared as a global variable that represents the entry to two functions/threads.
I want it to be accessed by the threads simultaneously (one for reading and one for writing/resizing). My program (in C++) includes a section where I check if the container is empty. Since one thread shows that the buffer is empty while the other desn't, it seems to me that both threads operate on two distinct containers although I defined only one.
#include "stdafx.h"
#include "ppl.h"
concurrent_vector<dataElm> ResultImage;
int AcquireImages(CameraPtr pCam){
continue_recording = true;
pCam->BeginAcquisition();
int imageCnt = 0;
while (continue_recording == true)
{
ImagePtr _p = pCam->GetNextImage(1000);
imageCnt = imageCnt + 1;
dataElm obj = constructelm(_p, &loc, imageCnt - 1);
ResultImage.push_back(obj);
cout << "is buffer empty? " << ResultImage.empty() << endl;
}
//...
}
void Cam(){
//...
pCam->Init();
INodeMap& nodeMap = pCam->GetNodeMap();
result = result | AcquireImages(pCam);
pCam = nullptr;
//...
}
void saveImages() {
//...
cout << "ResultImage.empty() = " << ResultImage.empty() << endl;
if (ResultImage.empty() == false) {
//saving the image
}
else
{
Sleep(20);
}
}
int main(int, char**){
std::thread producer(Cam);
std::thread consumer1(saveImages);
producer.join();
consumer1.join();
return 0;
}
Also, do I need to add synchronization primitives even though I'm using concurrent_vector?
I'm new to multi-threading so I'm sorry if my question seems stupid and excuse my english, i'm not american native.