sml icon indicating copy to clipboard operation
sml copied to clipboard

Dispatching events in separate thread spawned from action?

Open lambdaknight opened this issue 2 years ago • 0 comments

I was playing with one of your tests and trying to do something and ran into a problem.

struct c {
    auto operator()() noexcept {
        using namespace sml;
        // clang-format off
        return make_transition_table(
            * "s1"_s + on_entry<sml::initial> / [this] { calls += "s1e|"; }
            , "s1"_s + event<ev1> / [this](sml::back::process<ev2, ev3> p) -> void {
                calls += "a1.begin|";
                std::thread t([&](auto&& processEvent)
                {
                    std::cout << "Pew!" << std::endl;
                    processEvent(ev2{});
                    std::this_thread::sleep_for(std::chrono::seconds(1));
                    std::cout << "Pew pew!" << std::endl;
                    processEvent(ev3{});
                }, p);
                t.detach();
                calls += "a1.end|";
            }
            , "s1"_s + event<ev2> / [this] { calls += "a2|"; }
            , "s2"_s + event<ev3> / [this] { calls += "a3|"; }
            , "s1"_s + unexpected_event<ev3> / [this] { calls += "err|"; } = X
        );
        // clang-format on
    }

    std::string calls{};
};

Godbolt: https://godbolt.org/z/5rG8E99o7

Sadly, when I run it, I get this:

[c][process_event] boost::sml::back::on_entry<boost::sml::back::_, boost::sml::back::initial>
[c][action] (lambda at /app/example.cpp:50:49) boost::sml::back::on_entry<boost::sml::back::_, boost::sml::back::initial>
[c][process_event] ev1
[c][action] (lambda at /app/example.cpp:51:37) ev1
Pew!
Pew pew!
s1e|a1.begin|a1.end|

Neither ev2 nor ev3 get processed by the state machine. Any idea how I can get this working?

lambdaknight avatar Jul 25 '23 20:07 lambdaknight