My device is a camera.
I want to be able to start thread one, and run something similar to a state machine for a device one in that thread. Same thing for device two.
So far I have all the control functions written for the device. I can run them in two different threads completely fine. What I need to do is be able to control them from the main thread.
For my control thread I created a while loop that is always true unless an atomic reference to a variable is passed that corresponds to break loop.
What I want to do from the main thread, is to send a command to thread one saying to capture a frame. It takes 10ms to capture a frame. Half way through capturing the first frame I want to send a command to thread two to capture a frame and keep on doing so.
void classA::Capture(WINUSB_INTERFACE_HANDLE * handle, std::atomic<int>& cap)
{
std::vector<byte> frame;
while (true)
{
if (cap == 1)
{
frame = CaptureFrame(*handle);
cap = 0;
}
else if (cap == 2)
}
}
void ClassA::run()
{
std::vector<WINUSB_INTERFACE_HANDLE> devices = GetHandles();
Initialize(devices[0]);
Initialize(devices[1]);
WINUSB_INTERFACE_HANDLE h1 = devices[0];
WINUSB_INTERFACE_HANDLE h2 = devices[1];
std::atomic<int> cap1{ 0 };
std::atomic<int> cap2{ 0 };
std::thread t1(&ClassA::Capture,this, &h1, &n1, ref(cap1));
std::thread t2(&ClassA::Capture,this, &h2, &n2, ref(cap2));
for (int c = 0; c < 2000; c++)
{
if (c % 2 == 0)
{
cap1 = 1;
Sleep(5);
}
else
{
cap2 = 1;
Sleep(5);
}
}
cap1 = 2;
cap2 = 2;
t1.join();
t2.join();
}
What ends up happening is, sometimes devices work in the right order and grab frames like so 121212, but sometimes a device will skip and do this 1211221. (by 1 i mean device one, 2 device 2, capturing order)
After looking around I found std::queue
but I'm not sure how I can implement it. Thank you very much. I am sorry for the confusion. Any help is appreciated.