Multithreaded architecture

Hi
I'm trying to create a multithreaded library that calculate mandelbrot sets

i would like the api to look something like this:
1
2
MandelBrotGenerator genr;
std:future<MandelBrotData> fMandelData = genr.GenerateBrot(1.34f, 2.34f);


That way the user can choose whether to get() the value right away or wait for it.

Currently the way the GenerateBrot function works is by creating a threadpool of 8 threads and adding tasks to it for each pixel in the set,
and in the end it joins all the threads (which blocks the main thread - this is what i would like to change).

I cant figure out how to change the function to return a std::future and not block the main thread (since it depends on 8 threads not just 1),

so i was just wondering if maybe somebody could give me a quick psudo code example of how i could go about structuring something like that?

i've been trying to think about solutions on my own but i cant think of any
i'm fairly new to multithreaded programming so maybe it has an obvious anwser, sorry.
Last edited on
I cant figure out how to change the function to return a std::future and not block the main thread
That's what a future is. It blocks until the future value is available.

You could allow the threads to update the pixels as they're calculated without waiting. If your viewport changes, you'd cancel outstanding calculations and generate new ones.
If you really want to "create a multithreaded library that calculate mandelbrot sets", and decided that you need a thread pool, then use one that already exists, such as boost.asio.

But indeed, whether using a pool or writing your own as an exercise, you correctly observe that it has to outlive the function call (technically you could detach all thread, but it's awful), and so your next step is to choose where it lives.

It could live in that object "genr" that you went through the trouble of creating: then you can shut down the pool in MandelBrotGenerator's destructor. Or it could live in some object in wider scope (could even be static), in which case "MandelBrotGenerator" isn't even needed.
Topic archived. No new replies allowed.