Thread termination and request loss.

Source: Internet
Author: User
Write a small one today ProgramWhen a specified message is received, a socket is created to connect to the specified host. Once it is connected, data is sent to and retrieved from the host. To avoid blocking, create a thread every time you need to connect to the host, and call connect () in the thread. After the connection is successful, send () to send some data, then, wsarecv () is called to initiate an asynchronous overlapped request and the thread exits. The result is that the thread responsible for monitoring the completed port does not have data at all, but only receives a socket disconnection event. No way, you have to track the data in one step. The strange thing is that everything works normally during one step tracking. The sent data is sent, and the received data is also received ...... After some debugging information is added, wsarecv () will return wsa_io_pending. The returned value indicates that the overlapping Io has been submitted, but it is not yet completed ...... The returned value is normal, but data is not received. </Div>

The only difference between a single-step trace and a normal operation is that the execution of each statement in a single-step trace is delayed, so wsarecv () we added a sleep (1000) to the front and ran the program. We were surprised to find that the data was received. The wsarecv () value printed by the debugging information was 0 ...... Why can I succeed with latency? I thought for a moment and suddenly came up with a piece of memory a long time ago: When the thread initiating the IO request is terminated, the IO request will be discarded by windows! This is almost certainly the reason for a burst of Ecstasy in my heart. Change it right away. CodeUse queueuserworkitem () and pass the wt_executeiniothread parameter to replace the self-created thread. After running, everything works normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.