Scheduler class for implementing consumer-producer threading. More...
#include <scheduler.hpp>
Public Member Functions | |
Scheduler (std::vector< Prob * > &prob) | |
Constructor. More... | |
~Scheduler () | |
Destructor. More... | |
bool | is_error (void) |
Return true on errors. More... | |
bool | is_running (void) |
Return true if scheduler is running. More... | |
uint32_t | get_solved_count (void) |
Return number of solved problems. More... | |
uint32_t | get_problem_count (void) |
Return number of problems. More... | |
template<class Cont1 , class Cont2 > | |
size_t | get_errors (Cont1 &e, Cont2 &pi) |
Fetch errors and indices of corresponding problems. More... | |
void | run (std::vector< Solv * > solv) |
Run threads with N solvers. More... | |
void | lock_mutex (void) |
Lock mutex for adding problems. More... | |
void | unlock_mutex (void) |
Unlock mutex. More... | |
bool | force_exit (void) |
Force exit from scheduler. More... | |
bool | wait_finish (void) |
Call for solvers to finish when problems are solved. More... | |
bool | finish (void) |
Wait for all problems to be solved. More... | |
Friends | |
class | Consumer |
Detailed Description
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
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().
Constructor & Destructor Documentation
◆ Scheduler()
|
inline |
Constructor.
Constructor for scheduler solving problems in vector prob.
◆ ~Scheduler()
Destructor.
Member Function Documentation
◆ finish()
|
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.
◆ force_exit()
|
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.
◆ get_errors()
|
inline |
Fetch errors and indices of corresponding problems.
- Parameters
-
e Container where errors are appended pi Container where problems are appended
- Returns
- Number of error problems added to containers
Errors are removed from Scheduler.
◆ get_problem_count()
|
inline |
Return number of problems.
◆ get_solved_count()
|
inline |
Return number of solved problems.
◆ is_error()
|
inline |
Return true on errors.
◆ is_running()
|
inline |
Return true if scheduler is running.
◆ lock_mutex()
|
inline |
Lock mutex for adding problems.
◆ run()
|
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.
◆ unlock_mutex()
|
inline |
Unlock mutex.
◆ wait_finish()
|
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.
The documentation for this class was generated from the following file: