
On Mon, 30 Dec 2024 at 06:37, Georg Gast via Boost
Hi, I would use asio for two things: 1. Classic asio operation like rs232 and tcp. They can be processed in the usual asio way. 2. I receive jobs from an external device. The device can only send one job at a time. The processing must be in the order of arrival. This would post function objects to a strand. Each device would have its own strand.
As I understand you and Vinnie that should work as expected.
Yes, although execution order is not part of the contract, this will work in practice. Unless the job itself yields execution (ie it is itself an asynchronous composed operation). In this case your jobs could run interleaved. If you want to run composed operations sequentially (with respect to each other) then you must coordinate this yourself via an “asynchronous mutex or semaphore”. Asio’s timer object can be used as a semaphore : - set to max timeout - use cancel_one() to release the next job. R
Thanks
Georg
29.12.2024 16:27:53 Christian Mazakas via Boost
: On Fri, Dec 27, 2024 at 8:33 PM Georg Gast via Boost < boost@lists.boost.org> wrote:
Hi, If I use a asio::strand and post() there multiple function objects, are they executed in the same order or is the order of execution not specified?
I think to use it as a work queue but I need them to be processed in sequence...
Depending on your definition of "processed in sequence", a strand may or may not be what you're looking for.
Strands are first-in-first-out but if your function objects are async in the Asio sense, you'll run into problems here.
Each item in the strand will run in post() order until either it completes or it calls a non-blocking Asio function, which means you can have interwoven function objects executing "out of sequence".
Strands are also for the multi-threaded io_contexts, to give safe access to I/O objects.
If you need just a plain FIFO work queue, Asio has some thread pool classes you can use. If you need to sequence a bunch of async function objects, you'll need something different.
- Christian
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost