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

task_queue()

Constructor.

template<class R, class... Args>
void add(task<R, Args...>& t)      // add a constructed task

void add(std::unique_ptr<detail::task_container>&& task) // add a task using its storage type (used internally)

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

Member variables

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

Example

async::task_queue queue;
int val = 0;
const int 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