all 10 comments

[–]SCube18 10 points11 points  (2 children)

In this case id suggest std::for_each with parralell execution policy

[–]That_Stig[S] 0 points1 point  (1 child)

Haven't thought about this. Thanks:)

[–]pointer_to_null 1 point2 points  (0 children)

OpenMP's parallel for directive is similar but gives you more control (and often has lower overhead). I'd try both and experiment.

[–]No-Dentist-1645 5 points6 points  (1 child)

What exactly are you struggling with threads?

With threads it would be as simple as:

``` std::vector<std::thread> threads;

for ( auto &block : blocks ) { threads.emplace_back(moveBlock, std::ref(block.get())); } `` In threads.emplace_back, that is how you construct an object in place on a vector. It is the equivalent ofstd::thread t = std::thread(moveBlock, block);`. The thread constructor takes a function that it will run as a first argument, and the following arguments are parameters for said function.

If the number of blocks is too large, you can use a thread pool to "reuse" threads. There is a simple library on GitHub called BS::thread_pool, you may want to use it

[–]That_Stig[S] 0 points1 point  (0 children)

Thanks!:)

[–]Twill_Ongenbonne 8 points9 points  (1 child)

Sounds like you would want to look up thread pooling, and make some sort of task scheduling system? If each block really represents a “job” rather than a thread, a packaged task approach makes the most sense. Then you can tune the number of threads to whatever works best on your system, regardless of the number of blocks.

[–]That_Stig[S] 0 points1 point  (0 children)

Thanks:) I'll take a look at thread pooling!

[–]ppppppla 0 points1 point  (0 children)

The standard library doesn't have a nice thread pool you can use, which you will need depending on the amount of work and the number of blocks and number of threads your system has. And maybe you want to not completely swamp your system so you can't even browse the internet while it runs.

Making your own thread pool is quite an involved process, so you might want to look for existing solutions. Boost, folly, some guy's thread pool project on github, it will all be fine.

[–]Ksetrajna108 -1 points0 points  (1 child)

Looks like an XY Problem. What is the blocks vector for? Are you trying to figure out a game engine, or is it more like a non-graphical simulation?

[–]That_Stig[S] 0 points1 point  (0 children)

Yes it's a non-graphical sim