1

I need an advice with the situation

I'm doing a udp async multicast transiver:

  • he can accepts data from multicast group
  • he can send data to multicast group

Data is sent in the following cases:

  • async_receive_from -> callback -> async_send (in the same thread)
  • by external timer -> async_send(in another thread)
  1. I use one socket for receive and send. Do I need synchronize async_receive_from and async_send_to from external timer? booast::asio doc says that objects are unsafe. My solution is use a ioserivce post/dispatch for external timer data, that async_send_to call in the same thread

  2. If I make two sockets - one for read, one for thread, who works this one multicast groud. Do I need synchronize this?

P.S. I'm using one thread ioservice::run

Ieshir
  • 31
  • 7

1 Answers1

1

As soon as an external timer is involved likely a strand is required for synchronization. The only exception is when the service is restricted to a single thread already - and the "external timer" trigger originates from that same thread. This is called the implicit strand.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you, some more info -> I using one thread ioservice::run 1) Why my solution with post is wrong? I think about data was keep in lambda and write, when asio call this 2) In asio doc > Synchronous send, send_to, receive, receive_from, and connect operations are thread safe with respect to each other What about async_read \ write? Do i need strand here or only strand for timer(s) – Ieshir Aug 27 '22 at 12:39
  • Who said your solution with post is wrong? For one thing, I don't see any code, so it's hard to tell whether something is wrong. Even if you have 1 thread tunning io_service::run, it implies that *external* calls must be from another thread so indeed you have to synchronize access to your shared IO objects. See also https://stackoverflow.com/a/12801042/85371 – sehe Aug 27 '22 at 22:24