Skip to content

Add in post callback function to simulation#18

Open
msatti-metrol wants to merge 3 commits into
fschuetz04:mainfrom
msatti-metrol:feature/post
Open

Add in post callback function to simulation#18
msatti-metrol wants to merge 3 commits into
fschuetz04:mainfrom
msatti-metrol:feature/post

Conversation

@msatti-metrol
Copy link
Copy Markdown
Contributor

This PR adds in simulation<>::post() intended as a convenience method for creating an event with callback added, plus scheduling it to run immediately.

Tests included and passing.

@fschuetz04
Copy link
Copy Markdown
Owner

I'm unsure whether this helps readability. As far as I understand it, both snippets should behave the same:

sim.post([&ran]() { ran = true; }, 2.0);
auto ev = sim.timeout(2.0);
ev.add_callback([&ran]() { ran = true; });

Personally, I think the second version is readable, while you need to know the post function to understand the first version.

What is the use case for having this special post function?

@msatti-metrol
Copy link
Copy Markdown
Contributor Author

msatti-metrol commented Mar 24, 2026

both snippets should behave the same

Correct, no difference.

What is the use case for having this special post function?

There was no specific use case for this, more so we are using the same pattern repeatedly in multiple codebases of ours that it made sense to introduce a convenience one-liner function for it:

    void Callbacks_t::StartBitTimerInternal()
    {
        m_bitTimer = m_simulationContext.post(
            [this]()
            {
                    ...
                    StartBitTimerInternal();
            },
            m_bitTimerPeriod.count());
    }

The post name comes from Boost::ASIO, but the functionality is similar to other event loop based frameworks. For example: python. Maybe a change to the name like python would work? call_soon() and call_later()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants