I am trying to save data using threads. I am very new to multithreading. The rough idea is one thread will put the raw dummy data packets (12 MB) into a circular queue. The other thread will capture webcam image (OpenCV using C++) and put it in another circular queue. Now a third thread will synchronise the dummy data packet and image using timestamps and dump the data (i.e. synchronised ) into SSD Via USB 3.1. The code structure is as below
datapacketthread()
{
While(true)
{
//Generated dummy data packet. Used malloc to allocate the required size.
//Get Timestamp
packetqueue.enqueue()
}
}
camerathread()
{
//Initialise camera
// Set resolution
While(true)
{
//Capture a frame
//Get Timestamp
cameraqueue.enqueue()
}
}
savethread()
{
//Suggest any good synchronising mechanism … (one dummy data packet and a matching camera frame taken at the time of dummy packet generation)
fwrite(dummypacket);
imwrite(image); // in JPG format, Size around 70kB,
}
Main()
{
//initialisations
//Calling all three threads simultaneously
}
Now I have implemented this code without any synchronisation. The dummy packets and images are getting stored but the frame rate is only 14-15fps on my Jetson Xavier. If I disable the camera thread and store only dummy packets the frame rate is around 28 fps.
I have done a Read Write Benchmark Test using IOZONE and the obtained write speed around 8Gbps.(More info on IOZONE: http://www.iozone.org/)
But when I have timed the fwrite and it is around 38 ms (For a 12MB file speed is 2.6Gbps) if the camera thread is off. But once I turn on the camera thread the fwrite time is jumping upto 70ms(For a 12MB file speed is 1.4Gbps) and hence decreasing my fps. Can anyone help me out with any possible reason or explanation?
Once I am able to store the data with frame rate above 25fps, I would like to synchronise both the dummy data packet and the camera frame. Can anyone suggest any good way to synchronise and then save these to SSD via USB 3.1?