Classes | Public Member Functions | Friends | List of all members
Scheduler< Solv, Prob, Err > Class Template Reference

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

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().

Constructor & Destructor Documentation

template<class Solv , class Prob , class Err >
Scheduler< Solv, Prob, Err >::Scheduler ( std::vector< Prob * > &  prob)
inline

Constructor.

Constructor for scheduler solving problems in vector prob.

template<class Solv , class Prob , class Err >
Scheduler< Solv, Prob, Err >::~Scheduler ( )
inline

Destructor.

Member Function Documentation

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
eContainer where errors are appended
pContainer where problems are appended
Returns
Number of error problems added to containers

Errors are removed from Scheduler.

template<class Solv , class Prob , class Err >
uint32_t Scheduler< Solv, Prob, Err >::get_problem_count ( void  )
inline

Return number of problems.

template<class Solv , class Prob , class Err >
uint32_t Scheduler< Solv, Prob, Err >::get_solved_count ( void  )
inline

Return number of solved problems.

template<class Solv , class Prob , class Err >
bool Scheduler< Solv, Prob, Err >::is_error ( void  )
inline

Return true on errors.

template<class Solv , class Prob , class Err >
bool Scheduler< Solv, Prob, Err >::is_running ( void  )
inline

Return true if scheduler is running.

template<class Solv , class Prob , class Err >
void Scheduler< Solv, Prob, Err >::lock_mutex ( void  )
inline

Lock mutex for adding problems.

template<class Solv , class Prob , class Err >
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.

template<class Solv , class Prob , class Err >
void Scheduler< Solv, Prob, Err >::unlock_mutex ( void  )
inline

Unlock mutex.

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.

Friends And Related Function Documentation

template<class Solv , class Prob , class Err >
friend class Consumer
friend

The documentation for this class was generated from the following file: