美文网首页
c++ 任务队列实现

c++ 任务队列实现

作者: help_youself | 来源:发表于2020-11-30 19:55 被阅读0次
#include <iostream>
#include <memory>
#include <functional>
#include <deque>
#include <utility>

namespace std
{
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}
}

class QueuedTask {
 public:
  QueuedTask() {}
  virtual ~QueuedTask() {}
  virtual bool Run() = 0;
};
template <class Closure>
class ClosureTask : public QueuedTask {
 public:
  explicit ClosureTask(Closure&& closure)
      : closure_(std::forward<Closure>(closure)) {}
 ~ClosureTask(){ std::cout<<"task dtor"<<std::endl;}
 private:
  bool Run() override {
    closure_();
    return true;
  }

  typename std::remove_const<
      typename std::remove_reference<Closure>::type>::type closure_;
};
template <class Closure, class Cleanup>
class ClosureTaskWithCleanup : public ClosureTask<Closure> {
 public:
  ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup)
      : ClosureTask<Closure>(std::forward<Closure>(closure)),
        cleanup_(std::forward<Cleanup>(cleanup)) {}
  ~ClosureTaskWithCleanup() { cleanup_(); }

 private:
  typename std::remove_const<
      typename std::remove_reference<Cleanup>::type>::type cleanup_;
};

// Convenience function to construct closures that can be passed directly
// to methods that support std::unique_ptr<QueuedTask> but not template
// based parameters.
template <class Closure>
static std::unique_ptr<QueuedTask> NewClosure(Closure&& closure) {
  return std::make_unique<ClosureTask<Closure>>(std::forward<Closure>(closure));
}

template <class Closure, class Cleanup>
static std::unique_ptr<QueuedTask> NewClosure(Closure&& closure,
                                              Cleanup&& cleanup) {
  return std::make_unique<ClosureTaskWithCleanup<Closure, Cleanup>>(
      std::forward<Closure>(closure), std::forward<Cleanup>(cleanup));
}






class Channel{
public:
    Channel(int a):a_(a){}
    ~Channel(){
        std::cout<<"channel dtor "<<a_<<std::endl;
    }
    int a_=0;
};
class Thread{
public:
template <class Closure,
          typename std::enable_if<!std::is_convertible<
              Closure,
              std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
void PostTask(Closure&& closure) {
  PostTask(NewClosure(std::forward<Closure>(closure)));
}

void PostTask(std::unique_ptr<QueuedTask> task){
    tasks_.push_back(std::move(task));
}
    void Run(){
        std::deque<std::unique_ptr<QueuedTask>> tasks;
        tasks.swap(tasks_);
        while(!tasks.empty()){
            tasks.front()->Run();
            tasks.pop_front();
        }
    }
    void AsynChannel(std::function<void(Channel*)> f){
        PostTask([this,f](){
                    std::cout<<"run task"<<std::endl;
                    auto channel=AllocateChannel();
                    f(channel);
                 });
    }
    Channel *AllocateChannel(){
        Channel *channel=new Channel(count_);
        count_++;
        return channel;
    }
private:
    std::deque<std::unique_ptr<QueuedTask>> tasks_;
    int count_=1;
};
class Request{
public:
    Request(Thread*thread):thread_(thread){}
    void Trigger(){
        thread_->AsynChannel([&](Channel*channel){
                                this->OnChannel(channel);
                                   });
    }
    void OnChannel(Channel*channel){
        std::cout<<"on channel: "<<channel->a_<<std::endl;
        channel_.reset(channel);

    }
    ~Request(){
        std::cout<<"Request dtor"<<std::endl;
    }
private:
    Thread *thread_=nullptr;
    std::unique_ptr<Channel> channel_;
};
int main(){
    std::unique_ptr<Thread> thread(new Thread());
    Request *request=new Request(thread.get());
    request->Trigger();
    request->Trigger();
    thread->PostTask([request]{
                        delete request;
                     });

    thread->Run();
    return 0;
}

相关文章

  • c++ 任务队列实现

  • c++ 实现队列

    相关资料: 用C++实现一个队列 数据结构代码实现之队列的链表实现(C/C++)

  • c++同步任务队列的实现

     代码来源[1]。  之前有分析过webrtc中invoke函数的实现[2]。[1] TaskLoop.cpp[2...

  • C++ 实现线程池

    c++实现一个线程池,首先需要有两个数据结构,一个是线程池,一个是任务队列。为了每个线程线程安全的从任务队列里面拿...

  • Redis入门(5) - 消息通知

    使用列表实现任务队列 优先级队列 按照规则订阅 Redis也可以作为任务队列。任务队列顾名思义,就是“传递任务的队...

  • C++队列缓存的实现

    C++队列缓存的实现 为什么使用队列缓存 c++的队列缓存主要用于解决大数据量并发时的数据存储问题,可以将并发时的...

  • Golang实现简单爬虫框架(5)——项目重构与数据存储

    前言 在上一篇文章《Golang实现简单爬虫框架(4)——队列实现并发任务调度》中,我们使用用队列实现了任务调度,...

  • ES6 -异步编程

    javascript 是基于单线程的,如果想实现多个任务,需要任务队列来实现。 事件模型:点击按钮触发事件队列,异...

  • 【TencentOS tiny】深度源码分析(3)——队列

    队列基本概念 队列是一种常用于任务间通信的数据结构,队列可以在任务与任务间、中断和任务间传递消息,实现了任务接收来...

  • 任务队列实现心得

    近日,研究了一下任务队列,于是想到了阻塞队列BlockingQueue,不得不提起到它的两个方法,put,take...

网友评论

      本文标题:c++ 任务队列实现

      本文链接:https://www.haomeiwen.com/subject/eqwinqtx.html