![]() |
PIP 5.5.3
Platform-Independent Primitives
|
Thread pool loop. More...
#include <pithreadpoolloop.h>
Public Member Functions | |
| PIThreadPoolLoop (int thread_cnt=-1) | |
| Contructs thread pool with threads count "thread_cnt". If "thread_cnt" = -1 then system processors count used. | |
| void | setFunction (std::function< void(int)> f) |
| Set threads function to lambda expression "f" with format [ ](int){ ... }. | |
| void | wait () |
| Wait for all threads stop. | |
| void | start (int index_start, int index_count) |
| Start functions execution with integer argument range from "index_start" to "index_start + index_count - 1". | |
| void | exec (int index_start, int index_count) |
| Start functions execution with integer argument range from "index_start" to "index_start + index_count - 1" and wait for finish. | |
| void | exec (int index_start, int index_count, std::function< void(int)> f) |
| Start functions "f" execution with integer argument range from "index_start" to "index_start + index_count - 1" and wait for finish. | |
Thread pool loop.
This class allow you parallelize loop.
This class designed to parallel "for(;;)" statement in very simple way. In constructor several threads created, then by setFunction() method you should pass body of your loop, and then call start() or exec(). Every thread take loop counter and execute your function until all counter range is passed.
Example:
Equivalent to:
Due to multithreading its very important to protect output data of loop body, use mutex. Also remember that execution order is undefined and you shouldnt use global variables in your function. Use local variables and lambda capture.