Public Member Functions |
| Scheduler (std::vector< Prob * > &prob) |
| Constructor.
|
| ~Scheduler () |
| Destructor.
|
bool | is_error (void) |
| Return true on errors.
|
bool | is_running (void) |
| Return true if scheduler is running.
|
uint32_t | get_solved_count (void) |
| Return number of solved problems.
|
uint32_t | get_problem_count (void) |
| Return number of problems.
|
template<class Cont1 , class Cont2 > |
size_t | get_errors (Cont1 &e, Cont2 &pi) |
| Fetch errors and indices of corresponding problems.
|
void | run (std::vector< Solv * > solv) |
| Run threads with N solvers.
|
void | lock_mutex (void) |
| Lock mutex for adding problems.
|
void | unlock_mutex (void) |
| Unlock mutex.
|
bool | force_exit (void) |
| Force exit from scheduler.
|
bool | wait_finish (void) |
| Call for solvers to finish when problems are solved.
|
bool | finish (void) |
| Wait for all problems to be solved.
|
template<class Solv, class Prob, class Err>
class Scheduler< Solv, Prob, Err >
Scheduler class for implementing consumer-producer threading.
Scheduler uses a manager thread and a given number of working threads for solving problems. Scheduler is templated with Solver, Problem and Error classes. Solver class has to provide an operator
void operator()(
Problem *p, int32_t pi )
to solve problem p with index location pi.
Error class has to have a default constructor. Scheduler catches the errors of this type from the working threads and saves caught errors in a container. If an error is caught, all the working threads are interrupted and problem solving is finished. The scheduler does indicate the error state by returning false from finish(). Error state can also be queried with is_error(). Errors can be fetched from the internal containers with get_errors().
The Scheduler can be used for static and dynamic problems. All threads can add problems. The problem vector is a shared resource so it must be protected with lock_mutex() and unlock_mutex().
Parallel processing is started with run() function and the end of processing can be waited with finish().
template<class Solv , class Prob , class Err >
bool Scheduler< Solv, Prob, Err >::finish |
( |
void |
| ) |
|
|
inline |
Wait for all problems to be solved.
Waits for all solvers to finish. Stops all threads and frees resources. Returns false if any error occured during last run. Otherwise returns true.
Solver is prematurely exited if an error occurs.
template<class Solv , class Prob , class Err >
bool Scheduler< Solv, Prob, Err >::force_exit |
( |
void |
| ) |
|
|
inline |
Force exit from scheduler.
Waits for the solvers to finish the problems under progress after, which all threads are stopped and resources freed. Returns false if any error occured during last run. Otherwise returns true.
template<class Solv , class Prob , class Err >
template<class Cont1 , class Cont2 >
size_t Scheduler< Solv, Prob, Err >::get_errors |
( |
Cont1 & |
e, |
|
|
Cont2 & |
pi |
|
) |
| |
|
inline |
Fetch errors and indices of corresponding problems.
- Parameters
-
e | Container where errors are appended |
p | Container where problems are appended |
- Returns
- Number of error problems added to containers
Errors are removed from Scheduler.
template<class Solv , class Prob , class Err >
bool Scheduler< Solv, Prob, Err >::wait_finish |
( |
void |
| ) |
|
|
inline |
Call for solvers to finish when problems are solved.
Waits for solvers to finish for 1 sec. Returns true if solver finished, false if not. The finish() function needs to be called after true to free the resources.