autoshutdown.cpp 985 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // Copyright (c) 2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. // This example is identical to hello_loopless, except that it demonstrates
  7. // how to autoshutdown supervisor when an actor is down
  8. #include "rotor.hpp"
  9. #include "dummy_supervisor.h"
  10. #include <iostream>
  11. struct hello_actor : public rotor::actor_base_t {
  12. using rotor::actor_base_t::actor_base_t;
  13. void on_start() noexcept override {
  14. rotor::actor_base_t::on_start();
  15. std::cout << "hello world\n";
  16. // no need of doing supervisor->do_shutdown()
  17. do_shutdown();
  18. }
  19. };
  20. int main() {
  21. rotor::system_context_t ctx{};
  22. auto timeout = boost::posix_time::milliseconds{500}; /* does not matter */
  23. auto sup = ctx.create_supervisor<dummy_supervisor_t>().timeout(timeout).finish();
  24. sup->create_actor<hello_actor>().timeout(timeout).autoshutdown_supervisor().finish();
  25. sup->do_process();
  26. return 0;
  27. }