Kai Petzke
Kai Petzke

Reputation: 2984

How to query the number of completion handlers waiting on a strand in Boost ASIO?

Is there a simple way to determine the number of completion handlers waiting on a particular Boost ASIO strand? I am aware, that that number will only be approximate in a multithreaded environment, as it can always happen, that a completion handler finishes in the nanosecond just after that counter got queried, but before the counter got returned. Or, likewise, other threads could post new work to this strand just after the counter has been queried.

But such an approximate number would already be enough for my application.

I understand, that I could add an std::atomic<std::size_t> variable to my application, which gets incremented, whenever something is posted to the strand, and which is decremented by each completion handler, that gets executed in that strand. But with timers, that sometimes get cancelled, and async sockets, which sometimes get closed unexpectedly, mixed in, I am quite worried, that such application based counters can easily get out of sync. So, is there an easy way to "ask ASIO itself", how much work is waiting on a partiular strand?

Upvotes: 3

Views: 426

Answers (1)

sehe
sehe

Reputation: 393537

There's no way for that. Keep in mind that strands are like hash buckets: queues can (and will) be shared among different strands beyond a reasonable number of unique strands.

I think with executors I suspect one can easily make an executor that tracks the on_work_started/on_work_finished calls. I don't have an example of that, but I'm positive I've seen one or more in the library examples/tests.

Upvotes: 2

Related Questions