Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting work across threads

Currently, I process frames read from a video one by one, then write them to file. This seems inefficient and slow, so I'd like to split the work across multiple threads.

My current code can be summarized like this:

for(long n = 0; n < totalframes; n++) {
    using(Bitmap frame = vreader.ReadVideoFrame()) {
        Process(frame); //awfully slow
        WriteToFile(frame);
    }
}

How can I load, say, four frames, process them in four threads, wait for all of them to finish, then write them to file? It's critical that the frames are written in the exact same order as they were in the video.

like image 444
Peter W. Avatar asked Dec 26 '22 02:12

Peter W.


1 Answers

You can process the frames with for example a Parallel.ForEach(). And then read them with an iterator block (IEnumerable<>).

But the writing needs a little more attention. Make sure you attach a number to each frame and at the end of processing, dump them in a BlockingCollection<T> . Start a separate thread (Task) to process the queue and write the frames in order. This is a classic n-Producer / 1-Consumer solution.

like image 193
Henk Holterman Avatar answered Jan 07 '23 01:01

Henk Holterman