0

I want to call multiple functions on the elements of a list with QtConcurrent::mapped. How can I do that? One way would be to to create a composite function and pass that to mapped. However I imagine that might have some drawbacks if the individual pieces of work take different amounts of time.

Is there a way to chain multiple calls to mapped? I would like it to apply the second function on an element as soon as the first function is finished. It should not process all elements with the first function first before starting with the second.

I'm looking for something like:

QtConcurrent::blockingMapped(QtConcurrent::mapped(images, scale), rotate);

What I don't want:

QtConcurrent::blockingMapped(images, scale);
QtConcurrent::blockingMapped(images, rotate);
jdm
  • 9,470
  • 12
  • 58
  • 110
  • 1
    You mentioned the composite function approach. Why not explore that. You could have a function called scaleAndRotate(image) which takes a single image. And then call this function from within another function that is passed to QtConcurrent::mapped(images, batchScaleAndRotate) where batch scale and rotate calls the scaleAndRotate for each image. You can emit a signal once each scaleAndRotate is done to handle the result elsewhere in your codebase. – Carlo M. Dec 30 '20 at 10:33
  • @Voreno the problem is that the individual functions take a different amount of time. First, I scan the image file and generate a placeholder, then I load it and do some transformations. I want (e.g.) 1-2 threads generating the placeholders as fast as they can, and 1-2 threads doing the full processing as fast as they can (which will be slower). The user will see the placeholdes load very fast, and the full images load slower, but starting right away. If I follow your suggestion, the placeholders will only appear as fast as the full images are loading. So what I probably need is a queue... – jdm Jan 01 '21 at 09:38
  • This looks like a classic producer/consumer problem so I don't think `QtConcurrent` is the way to go here. Using a threadsafe queue -- as you've suggested -- should be more suitable. – G.M. Jan 02 '21 at 11:25

0 Answers0