PDA

View Full Version : Queued connection / Signal accumulation



pokey909
18th March 2008, 17:34
I am writing a computer vision program and want to use multiple threads with event loops to process independant computation tasks at once.
So its got this structre:



( Thread 1 ) ( Thread 2 ) __________
_________ ___> | Module A1 |-->| Module A2 |-->| Module A3 |----->| Thread 4 |
| Camera |____| | OpenGL |
| Grabber| | | GUI |
---------- ---->| Module B1 | -->| Module B2|---------------------> -----------
( Thread 3 )

Thread 2 and 3 connect to the camera thread's signal newFrame(Image*) so they start processing their stuff independently.
Thread 2 and 3 are also synchronized in such a way, that they only start to process a new fram if both of them finished their previous computation, otherwise thread 3 would process the next frame while thread 2 does not have the result yet and they become out of sync.

Now the question! If all signals get queued, what happens if the threads take longer to process than the camera thread sends frames? Will they keep accumulating in the event queue and i will always lag behind the current frame? If the answer is yes, is there a way to dequeue or overwrite pending events with a more recent one?

wysota
18th March 2008, 19:42
I think a better way is to make sure events are processed fast enough. Once you receive an event, store its data in some buffer. At that point you can decide to discard some lagging data. Then quickly return from the handler so that other events can be processed. Then use a timer that will be fetching frames at a specified frequency and process them there. Even if you get a lag, all "overflowing" events can discard their data (or some other data waiting to be processed) and everything should work. Just be sure to synchronize the buffer if you wish to use multiple threads for that.