What data structure (?) am i looking for here?

Hi,

i have this scenario in mind and i think it's really likely there's an implementation for this already but i don't know what it's called:

I'm reading video frames from a usb camera using the open_cv crate. i want to do some processing, and then append the processed frames to an ordered collection. the processing is so expensive that when i do this with a single thread, it will decrease the framerate at which i can record. so i want to do the processing in a thread pool. however, i want to make sure that the processed frames come out of the thread in order, i.e. they are inserted into the ordered collection in the same order they came in originally, even though processing might be faster/slower for some frames. Additionally, in case the thread pool is running at full capacity, it's ok to block the thread trying to insert new frames.

What am i looking for here?
Kind regards :slight_smile:
mmmmib

So you want to reorder the responses from the thread pool? Here are a few approaches:

  1. For every frame, the spawned task on the thread-pool should have a channel dedicated for that response. You can now put the response channels in a VecDeque and just wait on the channels in order.
  2. Together with the response, send an index corresponding to the order you want them in. You can then insert the responses into a BinaryHeap, which will allow you to pop them off in order.

As for spawning tasks on the thread-pool, you can use a crossbeam channel of bounded capacity. This channel allows several receivers, so each thread in the thread-pool can have a receiver for new tasks. Of course, you thread-pool library may also supply some mechanism for spawning tasks.

2 Likes

Thank you very much! i think i'll go with the VecDeque/channel solution :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.