Click here to Skip to main content
15,887,896 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
AnswerRe: WCHAR copy one char at a time Pin
Jeremy Falcon12-May-14 6:08
professionalJeremy Falcon12-May-14 6:08 
QuestionPlease describe overlapped operations Pin
bkelly1319-Jan-14 9:16
bkelly1319-Jan-14 9:16 
AnswerRe: Please describe overlapped operations Pin
Richard MacCutchan20-Jan-14 0:18
mveRichard MacCutchan20-Jan-14 0:18 
GeneralRe: Please describe overlapped operations Pin
bkelly1320-Jan-14 3:46
bkelly1320-Jan-14 3:46 
GeneralRe: Please describe overlapped operations Pin
Richard MacCutchan20-Jan-14 6:38
mveRichard MacCutchan20-Jan-14 6:38 
GeneralRe: Please describe overlapped operations Pin
bkelly1320-Jan-14 9:12
bkelly1320-Jan-14 9:12 
GeneralRe: Please describe overlapped operations Pin
Richard MacCutchan20-Jan-14 22:12
mveRichard MacCutchan20-Jan-14 22:12 
GeneralRe: Please describe overlapped operations Pin
pasztorpisti4-Apr-14 10:25
pasztorpisti4-Apr-14 10:25 
Overlapped vs Async: I guess you have already programmed multithread apps for windows. Then you have probably met some synchronizations objects like CritcalSection, Mutex, Slim RWLock, ... Do you see, here MS used basically synonyms to differentiate lock types provided by the OS. Generally we could say that all of these are mutexes if we use the word "mutex" out of the context of the windows api documentation but the winapi documents use the word "Mutex" to refer to one of the winapi locks (the one created by CreateMutex) that can have a name and can be shared between processes in return for some performance penalty.

After the previous paragraph it will be easier to understand the following: In the socket api docs MS uses the word "async" and "asyncblahblah" to differentiate a set of its socket handling apis from the rest of the apis. Usually with "async" the MS docs refer to the socket handling apis that send you completion callback as a window message (WSAAsyncSelect) but sometimes some docs also use "async" to refer to the bsd compatible select() function call and its more effective windows "equivalent" WSAEventSelect+WSAWaitForMultipleObjects or WSAPoll. But this is true only if we are in the context of the windows api docs and this is quite confusing. If we use async outside the context of the socket winapi docs then we can say that overlapped is also an async operation, the winapi docs just use "async" and "overlapped" to differentiate two different set of apis, its still very confusing as an overlapped operation is also an async operation. I guess this unfortunate naming convention probably comes from the fact that the original "async" api existed earlier than the overlapped stuff.

If you search for "asynchronous" you will find a lot of explanations and some of them are so theoretically so abstract that they are totally impossible to practically interpret in my opinion. Here is a very dirty and rough explanation what async means when it comes to programming: An operation is async if it can be started without blocking the starter thread after starting the operation. The thread just starts the operation and later this thread or another one can poll whether the execution has ended or there is another more effective way to detect the operation finish: waiting for it. There is another mechanism to detect the operation finish event: a callback is called by a separate thread when the operation has finished but the pure waiting and the callback can be converted one to the other with user code so they are basically equivalent from some point. Note that the implementation of an async operation is often a background thread, for example your own thread or an OS thread or a set of these threads but the implementation of an async operation can also be a hardware implementation (for example a DMA transfer whose completion may be polled or it may be signaled by the hardware for example in the for of an interrupt) or an arbitrary mixture of all the previously listed options. The async operation is async if we are taking a look at it from the perspective of the thread that starts the operation but it isn't async if we are looking at it from the perspective of the thread or hardware that actually performs the operation.

So if we take a look at the previous explanation you see that overlapped operations are indeed async operations and besides this overlapped IO is the most effective IO mechanism of windows. You can start it and you can poll for the result or you can later blocking-wait for the result. From the perspective of the operation-starter thread a blocking call can also be treated as a special case of an async (maybe an overlapped) operation: A blocking call starts the operation and immediately starts blocking-wait for its completion.

If you want to exploit the full power of windows then you wait for the end of many overlapped operations with a single IO completion port (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx[^]). The idea behinde IO completion ports is simple: You create an IO completion port and you register many overlapped operations to this "IO completion port", when you start an overlapped IO operation you just register it to this port. When windows finishes with one of the registered overlapped operations then it places a message into the queue of the IO completion port about the result. Besides this what you have to do is creating a thread pool and all threads should blocking-wait on the IO completion port handle for a new incoming message. When one message arrives then a thread grabs it and performs the necessary work (for example if socket/file/pipe/... write has finished then it checks whether you still have writeable data for that handle and if so then it starts another overlapped operation for that handle). Note that the linux epoll api and the bsd kqueue api also works with the same registration and operation-finished-queue concept but the windows api is much more complex because it is mixed with the overlapped api that is terribly easy to put wrong and buggy.

Note that you can use a single IO completion port and a generalized thread pool to perform overlapped read/write operations on many different kind of handles (files/sockets/pipes), this is why WriteFile/ReadFile works also on socket handles just like WSASend/Recv. Besides this no other socket handling api comes close to the scalability of IO completion ports when it comes to handling IO on a lot of handles.

SO_RCVBUF and SO_SNDBUF to zero? Lets say you are writing a file with overlapped IO. Then the overlapped operation basically writes the buffer you supplied into the disk storage occupied by the file or if caching is enabled windows copies the contents of your buffer into the IO cache and then it marks your write operation as finished immediately even before writing the contents to the disk. The same may happen with a blocking file write operation if windows has a lot of disk cache space. You can think of the socket SO_SNDBUF in a similar way as about the IO cache of disk files. Even a blocking send() call can return immediately if you supply a buffer whose size is less than or equal to the size of the free space of the SO_SNDBUF of the socket handle, in this case windows just appends your buffer contents to the end of the SO_SNDBUF of the socket handle and returns immediately. This is true for both blocking and overlapped operations, you are just writing this buffer and not actually sending data with your blocking send() call or your overlapped write operation. Then in the background the network stack performs the actual transfer over the wire in the background...

If you set SO_RCVBUF and SO_SNDBUF to zero then your socket handle basically doesn't have this caching area and in this case windows sends the data over the wire directly from your buffer and your overlapped operation finishes only when all data has been sent. In case of overlapped operations this isn't a big problem because you probably don't care whether an overlapped operation is in progress because it would block on a full SO_SNDBUF or on actually sending the data through the wire. SO_SNDBUF is an extreme optimization that isn't even available on other platforms and if you set the SO_SNDBUF to zero then you have more control over the buffer sizes from which the network stack is working and this can be used for both good and bad, be careful with it.

There is no golden rule to write the "best" networking engine. What is "best" and "optimal" is dependent on the particular problem to be solved and on the target hardware. You can never write something that is the "best" solution when it comes to networking.
GeneralRe: Please describe overlapped operations Pin
pasztorpisti4-Apr-14 10:26
pasztorpisti4-Apr-14 10:26 
SuggestionRe: Please describe overlapped operations Pin
Richard MacCutchan4-Apr-14 23:15
mveRichard MacCutchan4-Apr-14 23:15 
QuestionWinsock Peer to Peer using UDP and TCP Pin
SD120810-Jan-14 4:27
SD120810-Jan-14 4:27 
AnswerRe: Winsock Peer to Peer using UDP and TCP Pin
Albert Holguin10-Jan-14 4:59
professionalAlbert Holguin10-Jan-14 4:59 
GeneralRe: Winsock Peer to Peer using UDP and TCP Pin
SD120810-Jan-14 5:08
SD120810-Jan-14 5:08 
GeneralRe: Winsock Peer to Peer using UDP and TCP Pin
Albert Holguin10-Jan-14 7:34
professionalAlbert Holguin10-Jan-14 7:34 
GeneralRe: Winsock Peer to Peer using UDP and TCP Pin
SoMad10-Jan-14 8:56
professionalSoMad10-Jan-14 8:56 
GeneralRe: Winsock Peer to Peer using UDP and TCP Pin
bkelly1319-Jan-14 9:28
bkelly1319-Jan-14 9:28 
Generallink error 1104 Pin
Prasun Hazra 5-Jan-14 8:15
Prasun Hazra 5-Jan-14 8:15 
GeneralRe: link error 1104 Pin
Richard MacCutchan5-Jan-14 22:16
mveRichard MacCutchan5-Jan-14 22:16 
GeneralRe: link error 1104 Pin
Prasun Hazra 7-Jan-14 23:10
Prasun Hazra 7-Jan-14 23:10 
GeneralRe: link error 1104 Pin
Richard MacCutchan7-Jan-14 23:25
mveRichard MacCutchan7-Jan-14 23:25 
AnswerRe: link error 1104 Pin
Albert Holguin8-Jan-14 17:11
professionalAlbert Holguin8-Jan-14 17:11 
QuestionHow to create and register two dlls with same name?(VS 2010) Pin
J_Me10-Dec-13 21:43
professionalJ_Me10-Dec-13 21:43 
AnswerRe: How to create and register two dlls with same name?(VS 2010) Pin
Richard MacCutchan10-Dec-13 22:21
mveRichard MacCutchan10-Dec-13 22:21 
GeneralRe: How to create and register two dlls with same name?(VS 2010) Pin
J_Me10-Dec-13 23:53
professionalJ_Me10-Dec-13 23:53 
GeneralRe: How to create and register two dlls with same name?(VS 2010) Pin
Richard MacCutchan11-Dec-13 0:12
mveRichard MacCutchan11-Dec-13 0:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.