Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another constructor for wrapped_env_t that waits completion of init-function #74

Closed
eao197 opened this issue Nov 9, 2023 · 1 comment
Assignees
Milestone

Comments

@eao197
Copy link
Member

eao197 commented Nov 9, 2023

There is a tricky moment with the use of wrapped_env_t constructor with init function:

so_5::mbox_t target_mbox;
so_5::wrapped_env_t sobjectizer{
  [&](so_5::environment_t & env) {
    env.introduce_coop([&](so_5::coop_t & coop) {
        target_mbox = coop.make_agent<my_agent>(...)->so_direct_mbox(); // (1)
      });
  }
};
so_5::send<my_message>(target_mbox, ...); // (2)

There is no guarantee that code at point (1) completes before invocation of so_5::send at point (2). Moreover, there could be a data race, because target_mbox may be modified at (1) while it's used at (2).

The correct way is:

std::promise<so_5::mbox_t> target_mbox_promise;
so_5::wrapped_env_t sobjectizer{
  [&](so_5::environment_t & env) {
    env.introduce_coop([&](so_5::coop_t & coop) {
        target_mbox_promise.set_value( coop.make_agent<my_agent>(...)->so_direct_mbox() ); // (1)
      });
  }
};
so_5::mbox_t target_mbox = target_mbox_promise.get_future().get();
so_5::send<my_message>(target_mbox, ...); // (2)

But such code is more complicated.

May be another constructor of wrapped_env_t can help?

Something like:

so_5::mbox_t target_mbox;
so_5::wrapped_env_t sobjectizer{
  so_5::wrapped_env_t::wait_init_completion, // This has to be added!
  [&](so_5::environment_t & env) {
    env.introduce_coop([&](so_5::coop_t & coop) {
        target_mbox = coop.make_agent<my_agent>(...)->so_direct_mbox(); // (1)
      });
  }
};
so_5::send<my_message>(target_mbox, ...); // (2)

The use of so_5::wrapped_env_t::wait_init_completion dictated to suspend the calling thread until init-function completes its work.

@eao197 eao197 added this to the SO-5.8.2 milestone Nov 9, 2023
@eao197 eao197 self-assigned this Nov 9, 2023
eao197 added a commit that referenced this issue Nov 16, 2023
@eao197
Copy link
Member Author

eao197 commented Mar 22, 2024

It's a part of SO-5.8.2 release.

@eao197 eao197 closed this as completed Mar 22, 2024
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

No branches or pull requests

1 participant