15 AsyncJobQueue

AsyncJobQueue.h

// AsyncJobQueue.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2021

#pragma once
#ifndef Ceda_cxThread_AsyncJobQueue_H
#define Ceda_cxThread_AsyncJobQueue_H

#ifdef CEDA_DEPRECATED

#include "ManualResetEvent.h"
#include "Ceda/cxUtils/CedaAssert.h"
#include <boost/asio.hpp>
#include <memory>

/*
Not used, probably should remove
*/

namespace ceda
{
template<typename Job>
class AsyncJobQueue final :  public std::enable_shared_from_this<AsyncJobQueue>
{
public:
    explicit AsyncJobQueue(boost::asio::io_context& context) :
        context_(context)
    {
    }

    void Start()
    {
        abort_ = false;
        auto self(shared_from_this());
        boost::asio::post(context_,
            [this, self, taskFn]()
            {
                started_ = true;
            });
    }

    void Stop()
    {
        abort_ = true;      // Make long running task abort
        ManualResetEvent e;
        auto self(shared_from_this());
        boost::asio::post(context_,
            [this, self, &e]()
            {
                started_ = false;
                event.Signal();
            });
        e.Wait();
    }

    void Push(Job job)
    {
        auto self(shared_from_this());
        boost::asio::post(context_,
            [this, self, job]()
            {
                if (started_)
                {
                    job(abort_);
                }
            });
    }

private:
    boost::asio::io_context& context_;
    std::atomic<bool> abort_ = true;
    bool started_ = false;
};

template<typename Job>
std::shared_ptr<AsyncJobQueue<Job>> MakeAsyncJobQueue(boost::asio::io_context& context)
{
    return std::make_shared<AsyncJobQueue<Job>>(context);
}

} // namespace ceda

#endif // CEDA_DEPRECATED

#endif // include guard