Support Class Library
A set of tools providing classes and utility
Active.h
Go to the documentation of this file.
1 #pragma once
2 #include <functional>
3 #include <thread>
4 #include <atomic>
5 #include <scl/async/Channel.h>
6 
7 
8 namespace scl{
9  namespace async{
13  class Active{
14  public:
19  using message_type = std::function<void()>;
20 
25  using thread_type = std::thread;
26 
31  using flag_type = std::atomic_bool;
32 
38 
39  protected:
44 
49 
54 
55  public:
59  Active() : done{false}, mq{}, thread{[=]{
60  while(!done){
61  auto message = mq.receiver().receive();
62  message();
63  }
64  }} {}
65 
70  this->execute([=]{
71  done = true;
72  });
73 
74  this->thread.join();
75  }
76 
81  void execute(message_type msg){
82  if(done)
83  return;
84 
85  this->mq.sender().send(msg);
86  }
87  };
88  }
89 }
flag_type done
The flag determining whether or not we should execute more tasks.
Definition: Active.h:43
Global namespace of the SCL.
Definition: alias.hpp:3
receiver_type & receiver()
Get a receiver for this channel.
Definition: Channel.h:362
void execute(message_type msg)
Execute a task via the active object.
Definition: Active.h:81
value_type receive()
Alias for ChannelReceiver::receive.
Definition: Channel.h:685
ChannelSender & send(U &&value)
Alias for ChannelSender::push.
Definition: Channel.h:557
message_queue_type mq
The queue used to transfer messages.
Definition: Active.h:48
Active()
Construct an active object (starts spinning the queue)
Definition: Active.h:59
A class that allow task execution on a separate thread using RAII semantics.
Definition: Active.h:13
thread_type thread
The worker thread used to execute tasks.
Definition: Active.h:53
std::thread thread_type
The type of threads used to execute the tasks.
Definition: Active.h:25
~Active()
Destroy an active object (finishing the queue)
Definition: Active.h:69
sender_type & sender()
Get a sender for this channel.
Definition: Channel.h:371
std::function< void()> message_type
The type of messages sent through the queue.
Definition: Active.h:19
std::atomic_bool flag_type
The type of flags used for completion checks.
Definition: Active.h:31