Scheduler class for implementing consumer-producer threading. More...
#include <scheduler.hpp>
Classes | |
class | Consumer |
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. | |
Friends | |
class | Consumer |
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().
Scheduler< Solv, Prob, Err >::Scheduler | ( | std::vector< Prob * > & | prob | ) | [inline] |
Constructor.
Constructor for scheduler solving problems in vector prob.
Scheduler< Solv, Prob, Err >::~Scheduler | ( | ) | [inline] |
Destructor.
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.
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.
size_t Scheduler< Solv, Prob, Err >::get_errors | ( | Cont1 & | e, |
Cont2 & | pi | ||
) | [inline] |
Fetch errors and indices of corresponding problems.
e | Container where errors are appended |
p | Container where problems are appended |
Errors are removed from Scheduler.
uint32_t Scheduler< Solv, Prob, Err >::get_problem_count | ( | void | ) | [inline] |
Return number of problems.
uint32_t Scheduler< Solv, Prob, Err >::get_solved_count | ( | void | ) | [inline] |
Return number of solved problems.
bool Scheduler< Solv, Prob, Err >::is_error | ( | void | ) | [inline] |
Return true on errors.
bool Scheduler< Solv, Prob, Err >::is_running | ( | void | ) | [inline] |
Return true if scheduler is running.
void Scheduler< Solv, Prob, Err >::lock_mutex | ( | void | ) | [inline] |
Lock mutex for adding problems.
void Scheduler< Solv, Prob, Err >::run | ( | std::vector< Solv * > | solv | ) | [inline] |
Run threads with N solvers.
Returns immediately after creating working threads. Use finish() or destructor of class to wait for work to be completed.
void Scheduler< Solv, Prob, Err >::unlock_mutex | ( | void | ) | [inline] |
Unlock mutex.
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.
friend class Consumer [friend] |