Threadsafe类通过套接字发送消息(Threadsafe class to send messages through socket)

编程入门 行业动态 更新时间:2024-10-25 06:25:13
Threadsafe类通过套接字发送消息(Threadsafe class to send messages through socket)

我正在尝试实现一个通过套接字向服务器发送消息的模块。 它将在多线程环境中使用,因此这个“Client”对象在线程之间共享。 我的问题是,我应该在Send方法中使用锁定块来使此类线程安全吗? (可能是的,但我看到了很多示例代码,没有任何锁定。)

这是我的MessengerClient类的简化版本。

public class MessengerClient { private Socket socket; public MessengerClient() { socket = new Socket(SocketType.Stream, ProtocolType.IPv4); } public void Connect(string host, int port) { socket.Connect(host, port); } public void SendMessage(IMessage message) { var buffer = ObjectConverter.ConvertToByteArray(message); var args = new SocketAsyncEventArgs(); args.Completed += args_Completed; args.SetBuffer(buffer, 0, buffer.Length); //lock (socket) //{ socket.SendAsync(args); //} } }

I'm trying to implement a module that sends messages to a server via socket. It will be used in a multi threaded environment, so this "Client" object is shared among threads. My question is that should I use a lock block int the Send method to make this class threadsafe? (Probably yes, but I saw a lot of sample codes, where there isn't any locking.)

Here is the simplified version of my MessengerClient class.

public class MessengerClient { private Socket socket; public MessengerClient() { socket = new Socket(SocketType.Stream, ProtocolType.IPv4); } public void Connect(string host, int port) { socket.Connect(host, port); } public void SendMessage(IMessage message) { var buffer = ObjectConverter.ConvertToByteArray(message); var args = new SocketAsyncEventArgs(); args.Completed += args_Completed; args.SetBuffer(buffer, 0, buffer.Length); //lock (socket) //{ socket.SendAsync(args); //} } }

最满意答案

根据Socket的文档 :

线程安全:

此类的实例是线程安全的。

因此,虽然线程安全是一个有点模棱两可的术语 ,但在这种情况下它意味着这个类的方法,包括实例方法,从你的角度看似是原子的。 你可以调用一个方法,并且知道它看起来好像完全在大约同一时间在其他线程中调用的任何方法之前或之后完全运行。 它不会执行一个方法的一半,然后是另一个,然后完成第一个(除非它可以保证在分割时输出与使它们完全原子相同)。

因此,简而言之,您不需要添加lock 。 Socket类将确保在[或大约]同时从多个线程调用方法时没有竞争条件。

As per the documentation for Socket:

Thread Safety:

Instances of this class are thread safe.

So while thread safe is a somewhat ambiguous term, what it means in this context is that methods of this class, including instance methods, appear atomic from your point of view. You can call a method and know that it will appear as if it ran entirely before or entirely after any methods called in other threads at around the same time. It won't ever execute half of one method, then another, then finish the first (unless it can guarantee that when splitting it up the output is the same as making them completely atomic).

So, in short, you don't need to add lock. The Socket class will ensure that there are no race conditions as a result of calling the method from multiple threads at [or around] the same time.

更多推荐

本文发布于:2023-08-03 07:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送消息   class   Threadsafe   socket   messages

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!