sobjectizer icon indicating copy to clipboard operation
sobjectizer copied to clipboard

Using C++20 coroutines to handle messages in agents

Open matveigavrilov1 opened this issue 3 months ago • 6 comments

Hello! Have you considered using coroutines to process messages in agents? It would be good solution for waiting messages with saving context.

Something like that

class SomeAgent: public agent_t
{
// ...

coro_type sendRequestWaitResponse()
{
    so_5::send<Request>(someMbox);
    auto response = co_await so_5::receive<Response>(someMbox);
    // handle response
}

//...
};

matveigavrilov1 avatar Nov 25 '25 08:11 matveigavrilov1

I tried to implement this in my application and found a solution, but I decided to abandon this idea because of one problem. When so_finish_agent is called, the agent can no longer receive messages, making it impossible to wait for all running coroutines to complete. Therefore, I concluded that if this should be implemented, it should be done at the library level.

matveigavrilov1 avatar Nov 25 '25 08:11 matveigavrilov1

Hi!

There is a couple of messages in Russian related to this topic:

https://www.linux.org.ru/forum/development/18146604?cid=18148015 https://www.linux.org.ru/forum/development/18146604?cid=18148025

In short: this topic is floating in the air and it seems it will be implemented somewhere in the future, but I can't say when.

eao197 avatar Nov 25 '25 08:11 eao197

Thank you for answer! Can I try to implement prototype and make PR? It would be great practice for me.

matveigavrilov1 avatar Nov 25 '25 08:11 matveigavrilov1

Can I try to implement prototype and make PR?

Of course, you can try, but I can't guarantee that it'll be accepted ;)

eao197 avatar Nov 25 '25 09:11 eao197

Ok, thanks. I just wanted to know that the PR would at least be reviewed)

matveigavrilov1 avatar Nov 25 '25 09:11 matveigavrilov1

I just wanted to know that the PR would at least be reviewed)

Of course it will.

Personally, I have no experience with C++20 coroutines yet. So if PR won't be accepted it will be useful to see how coroutines can be used in SObjectizer implementation.

eao197 avatar Nov 25 '25 09:11 eao197