// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
#pragma once
#include "ConcurrentQueue.h"
#include "Task.h"
///
/// Lock-free implementation of thread-safe tasks queue.
///
template
class ConcurrentTaskQueue : public ConcurrentQueue
{
public:
///
/// Adds item to the collection (thread-safe).
///
/// Item to add.
FORCE_INLINE void Add(T* item)
{
ConcurrentQueue::enqueue(item);
}
///
/// Cancels all the tasks from the queue and removes them.
///
void CancelAll()
{
T* tasks[16];
std::size_t count;
while ((count = ConcurrentQueue::try_dequeue_bulk(tasks, ARRAY_COUNT(tasks))) != 0)
{
for (std::size_t i = 0; i != count; i++)
{
tasks[i]->Cancel();
}
}
}
};