Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

unpause::async::task_queue

james h edited this page Aug 12, 2017 · 10 revisions

task_queue

A simple queue that stores tasks to be run in order.

Member Functions Ctor & Dtor

task_queue();
~task_queue();

Add a task to the queue

// add a constructed tasktemplate<classR, class... Args>
voidadd(task<R, Args...>& t) ;
// add a task using its storage type (used internally)voidadd(std::unique_ptr<detail::task_container>&& task) ;
// construct and add a tasktemplate<classR, class... Args>
voidadd(R&& r, Args&&... a) ;

Running tasks and manipulating the queue

// Run the next task and return true, or if there are no more tasks return false.boolnext() ;
// returns true if there are tasks in the queue and false if not.boolhas_next() ;
// return the next dispatch time for the next task // or std::chrono::time_point::min if none
std::chrono::steady_clock::time_point next_dispatch_time() ;
// Get the next task and pop it off the queue
std::unique_ptr<detail::task_container> next_pop() ;
// sort the queue with a predicatevoidsort(std::function<bool(const detail::task_container& lhs, const detail::task_container& rhs)> predicate) ;

Member variables

// used to protect the underlying std::deque 
std::mutex task_mutex; // marks the queue as complete and no more tasks will run.
std::atomic<bool> complete;

Example

async::task_queue queue;
int val = 0;
constint n = 10000;
for(int i = 1 ; i <= n ; i++) {
queue.add([&](int in) { val += in; }, (int)i);
}
while(queue.next());
printf("val=%d n=%d t=%d\n", val, n, (n*(n+1)/2));
assert(val==(n*(n+1)/2));

Result:

val=50005000 n=10000 t=50005000

Clone this wiki locally