Scenario:
Inside object A (thread A), boost::asio::ip::tcp::socket is being read from and written to asynchronously.
Object B (thread B) posts data to object A's data queue.
Object A should write the data in its data queue as soon as possible.
How to achieve the third point efficiently?
Right now I'm doing this:
There might be no data in the queue.
socket->async_send(data, handler);
inside handler: back to point two.
I'm worried that this approach is highly inefficient - calling async_send with zero-length data most of the time until actual data can be sent.
Might it be that a better approach would be to have an additional thread inside object A that performs synchronous writes on the socket as soon as new data is posted? Peforming the write from object B's thread is out of question.
Well firstly, unless you have a good reason to do I personally wouldn't break it down into 1 thread per object.
Instead, have a shared io_service
(just pass it in by reference to both A and B ctors. Then have a single thread on the io_serice.run()
.
Assuming one of the objects is also async_reading, you needn't be writing 0 length datums and creating a loop in the handler. Just schedule the async_write
as an when data comes in.
“ 对象A应该尽快将数据写入其数据队列中 ”可能被理解为等待C ++的未来,因此您检查答案并确认boost :: asio :: example ,最后但并非最不重要的一点是,我认为有一些改进您的“ 数据队列 ”中将要求您可以看看该答案 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.