Is Winsock thread-safe?

Source: Internet
Author: User
Tags sendmsg

On Modern windows stacks, yes, it is, within limits.

It is safe, for instance, to have one thread callingSend ()And another thread callingRecv ()On a single socket.

By contrast, it's a bad idea for two threads to both be callingSend ()On a single socket. this is "thread-safe" in the limited sense that your program shouldn't crash, and you certainly shouldn't be able to crash the kernel, Which is handling theseSend ()CILS. The fact that it is "safe" doesn't answer key questions about the actual effect of doing this. Which call's data goes out first on the connection? Does it get interleaved somehow? Don't do this.

Instead of multiple threads accessing a single socket, you may want to consider setting up a pair of network I/O queues. then, give one thread sole ownership of the socket: This thread sends data from one I/O queue and enqueues received data on the other. then other threads can access the queues (with suitable synchronization ).

Applications that use some kind of non-synchronous socket typically have some I/O queue already. of particle interest in this case is overlapped I/O or I/O Completion Ports, because these I/O strategies are also thread-friendly. you can tell Winsock about several overlapped blocks, and Winsock will finish sending one before it moves on to the next. this means you can keep a chain of these overlapped blocks, each perhaps added to the chain by a different thread. each thread can also callWsasend ()On the block they added, making your main loop simpler.

Certificate --------------------------------------------------------------------------------------------------------------------------------------

AtomicitySendmsg ()Family of functions

System Atomicity
AIX 5.2 Yes
FreeBSD 6.1 No
HP-UX B .11.23. No
IRIX 6.5.27 No
Linux 2.6.x No
Mac OS X 10.4 Tiger No
Solaris 10 Yes
Windows 2000/XP Yes, but with caveats

Http://www.almaden.ibm.com/cs/people/marksmith/sendmsg.html

Http://tangentsoft.net/wskfaq/intermediate.html#threadsafety

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: info-contact@alibabacloud.com 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.