yat  0.21pre
Classes | Public Member Functions | List of all members
theplu::yat::utility::Scheduler Class Reference

Handle a number of jobs and send them to threads. More...

#include <yat/utility/Scheduler.h>

Classes

class  Job
 

Public Member Functions

 Scheduler (unsigned int threads)
 constructor More...
 
void add_dependency (boost::shared_ptr< Job > job, boost::shared_ptr< Job > prerequisite)
 add a dependency rule More...
 
void interrupt (void)
 interrrupt all jobs
 
size_t jobs (void) const
 
void submit (const boost::shared_ptr< Job > &job)
 submit a job to Scheduler More...
 
unsigned int threads (void) const
 
void threads (unsigned int n)
 
void wait (void)
 wait for all jobs to finish
 

Detailed Description

Handle a number of jobs and send them to threads.

Scheduler starts a (user defined) number of threads and handles a series of Jobs. The Jobs can have dependencies, such that Job X must finish before Job Y can run, and the Scheduler takes care of these dependencies. Here is a small code example in which two threads are used to process the four jobs, MyJob. The jobs have a dependency that job4 can not run until the three first jobs have completed.

Scheduler scheduler(2);
boost::shared_ptr<MyJob> job1(new MyJob("Hello"));
boost::shared_ptr<MyJob> job2(new MyJob(" "));
boost::shared_ptr<MyJob> job3(new MyJob("World"));
boost::shared_ptr<MyJob> job4(new MyJob("\n"));
scheduler.add_dependency(job4, job1);
scheduler.add_dependency(job4, job2);
scheduler.add_dependency(job4, job3);
scheduler.submit(job4);
scheduler.wait();

The Scheduler sends jobs to the workers taking into account dependencies, priorities and the order in which the Scheduler has seen the Jobs. If the Jobs that are ready to run, i.e., all jobs it depends on have completed, the Scheduler choose the Job with highest priority. If two Jobs have the same priority, the Scheduler sends them in the order of appearance, i.e., if the Scheduler saw Job X before Job Y, Job X is run before Job X.

Note
In the current implementation all submitted jobs have to be completed before the Scheduler goes out of scope, which can be accomplished by calling wait() or interrupt().
Prior version 0.17 the Scheduler could not be recycled, i.e., if wait(void) or interruped(void) had been called, the behaviour of submit() was undefined.
Since
New in yat 0.13

Constructor & Destructor Documentation

◆ Scheduler()

theplu::yat::utility::Scheduler::Scheduler ( unsigned int  threads)

constructor

Parameters
threadsnumber of threads that are used

Member Function Documentation

◆ add_dependency()

void theplu::yat::utility::Scheduler::add_dependency ( boost::shared_ptr< Job job,
boost::shared_ptr< Job prerequisite 
)

add a dependency rule

Add a dependency that Job prerequisite has to complete before Job job is run.

Note, job cannot have been submitted, neither directly or by being prerequisite of a submitted job.

◆ jobs()

size_t theplu::yat::utility::Scheduler::jobs ( void  ) const
Returns
Number of jobs that are either running or queued, i.e., jobs that are waiting for a dependency are not counted.
Since
New in yat 0.15

◆ submit()

void theplu::yat::utility::Scheduler::submit ( const boost::shared_ptr< Job > &  job)

submit a job to Scheduler

If job depends on other jobs, they are also submitted to the Scheduler.

◆ threads() [1/2]

unsigned int theplu::yat::utility::Scheduler::threads ( void  ) const
Returns
number of threads used
Since
new in yat 0.17

◆ threads() [2/2]

void theplu::yat::utility::Scheduler::threads ( unsigned int  n)

Change number of threads used. If n is greater than current number of threads, additional threads are launched. If n is smaller than current number used, a number of threads are notified to end after the current job has been completed. A decrease in threads therefore does not have an immediate effect.

Since
new in yat 0.17

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

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14