last day (16 days later) » 

2:59 AM
0
Q: boost::asio readsome function does not seem to work

Cyborg-X1I am currently experiencing troubles using boost::asio, I want to write and read like in the following piece of code. Write works fine, but reading always returns zero. Actually I also found out that every site refers to the read_some function instead of the readsome function I have ...

 
Having had that discussion a few times on the job, while I failed there to convince others, I highly doubt that non-blocking is the way to go. Not only because it ends up in spinning threads which could otherwise sleep, which is wasteful (energy, trees, ...). Also, I cannot fathom why so many think non-blocking IO facilitates things.
 
@BitTickler Non-blocking I/O does not imply or require spinning. Perhaps you are conflating it with polling?
 
@rhashimoto Non-blocking is polling basically - how else would you know data is available?
 
@BitTickler The same way blocking calls are ultimately notified: by device interrupt.
 
@rhashimoto In most modern OS, an interrupt causes a interrupt service thread (or microthread) to be scheduled. That threat is also not polling the interrupt status - the ISR interacts with the scheduler. In the same sense, user space threads are either in state "running" or "waiting" (or other states, here not relevant). If you do blocking call, the thread transits to "waiting" and once the event is triggered, the scheduler places the thread back into the running queue. If you do non-blocking IO the thread state is not affected.
 
2:59 AM
@BitTickler Yes...that's what I said - blocking calls are ultimately notified by device interrupt. Non-blocking calls are also notified by device interrupt. The interrupt may result in setting a flag which user-level code can poll. But the interrupt can instead result in invoking a user-supplied handler - that's non-blocking I/O without polling. And good asynchronous I/O code typically never spins. It generally still blocks; it just blocks when it decides that it can't proceed. Have you actually used boost::asio (in an asynchronous manner)?
The advantage of non-blocking I/O is not that you don't block - that would waste CPU. It's that you get to choose when and where to block. boost::asio programs are generally quite efficient, and I've certainly never written (or even seen) one that spins.
 
 
3 hours later…
5:36 AM
I see 2 scenarios: 1. Bare metal programming without OS. Even there, usually, the main() "thread" polls IO, involving interrupts. Passing a "handler function" to the ISR so it calls in the context of the interrupt handler is not usualy good practice. 2. OS (e.g. *nix, Windows, QNX, Windows CE, TEngine, ....
In spite of minor differences, everything in such a process can only execute code in the context of a thread. A minor beinding to that is the vintage Unix V signal execution model, where the main thread is switching to a second run time context and then executes a signal handler function.
And this is actually similar to what asio does in my understanding. It has a "cooperative task" model implementation. You can call it coroutines or "tasks" and some soft task scheduling mechanism.
Only because asio abstracts away from the core OS mechanics does not mean that there is ever something else but a thread which either has to poll or block inside asio (why then non-blocking?). If you write applications in that model, it might look as if you never poll or never block but actually, this is just because of the tasks and activation framework hiding what really happens.
 
 
8 hours later…
1:36 PM
No one said that boost::asio doesn't use threads. No one said that programs that use non-blocking I/O never block. Your contention was that non-blocking I/O causes spinning. It doesn't, and I can't find anything in what you have written that supports that it does. boost::asio programs do not spin unless they deliberately intend to or are poorly written.
 
 
3 hours later…
4:21 PM
So what is the benefit of setting a socket to non-blocking if not to avoid a thread blocking? I see none. And whenever people want to keep their (single) thread going, they either want to avoid context switches (high performance edge cases) or they want to keep their design (which has to handle multiple asynchronous IO sources) single threaded.
The guy I had this discussion with at my job at least argued along the line "No multi threading is why I want to use it". Me, personally, I could not see with that postulate in mind, what else but spinning and polling he could accomplish. If he were using asio in a way where asio internally uses threads, he would violate his own postulate and I would see my point of not using asio for that context confirmed. As he denied that, I could not follow his reasoning for using it in the first place.
 
 
1 hour later…
5:32 PM
My understanding of your argument is that because you couldn't understand how one person you spoke with could avoid spinning, that implies that non-blocking I/O causes spinning. My argument is that you would be very hard-pressed to find an example of an asynchronous boost::asio program that spins. And the advantage of non-blocking I/O is that it gives you the flexibility to block exactly when you want to block instead of being forced to block on each I/O operation.
Using this flexibility comes with some disadvantages as well. It is a trade-off. But I don't see how you can reasonably argue that no one could find a benefit for this, especially among the many people who apparently have.
I would challenge you to write an asynchronous boost::asio program that spins on I/O. I don't think it's that easy.
 

  last day (16 days later) »