简体   繁体   English

同步套接字send()

[英]Synchronizing socket send()

Alright so basically I have 3 threads. 好吧,基本上我有3个线程。

Each one of them use a function which inside uses the send() on a specific socket (let's call it S). 它们每个都使用一个函数,该函数在内部使用特定套接字上的send()(我们称其为S)。 Here's a small design: 这是一个小设计:

http://i.imgur.com/5N744.png

How would I synchronize the S socket in such a way so that send() will be called one after another in a queue instead of the threads accessing the socket all at the same time? 我将如何以这种方式同步S套接字,以便在队列中一个接一个地调用send()而不是同时访问所有套接字的线程?

First, why not have an actual queue of data to be sent, and a dedicated IO thread popping items off of it and writing to the socket? 首先,为什么不发送实际的数据队列 ,并使用专用的IO线程从中弹出项目并写入套接字?

Then, if you are using regular blocking socket semantics, you can just write to the same socket from all three threads concurrently - the kernel will provide required locking at the system call level. 然后,如果使用常规的阻塞套接字语义,则可以同时从所有三个线程写入同一套接字-内核将在系统调用级别提供所需的锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM