Can you explain me on how to do that i.e. How to ask the main function to periodically check if new UDP data has been received while running the main loop continuously ? |
I can't give you specifics without seeing some reference docs on whatever API you're using. Though conceptually it's as simple as this:
1 2 3 4 5 6 7 8 9 10 11
|
while(whatever)
{
if( isIMUDataAvailable() )
{
processIMUData();
}
if( isCameraDataAvailable() )
{
processCameraData();
}
}
|
The only thing is that the isXDataAvailable functions must be
non-blocking. That's really the crux of it.
If I check the main loop only once (lets say at the start of the while loop), as the camera takes 30ms, I lose data from the IMU. |
You're calling a function to check to see if the camera has data, right? Is that function blocking?
A "blocking" function will wait until data is available, which prevents your program from doing other things.
A "non-blocking" function will return immediately even if no data was available, which will allow you to do something else.
If you want to do this in a single thread, you will need a non-blocking interface.
I think try_lock would should work but never tried it before. Do you think it will be fast ? |
try_lock is a non-blocking mutex lock. If it can't immediately lock the mutex, it won't wait... it'll just return immediately (but the mutex won't be locked). So yes it might be faster... but it'll only be faster when it fails.
Though it might still have to interrupt the pipeline and do memory sync stuff... so it might not even be faster. I dunno... try it and see.
EDIT:
I just want to clarify --- I'm not saying that multithreading is a bad choice here. It might be the right way to go. But if you don't want it to be slow you'll have to cut back on how frequently you synchronize threads.