如何使用多线程,锁,套接字编程 [英] How to use Multithreading, locks, Socket Programming
问题描述
在过去的48个小时中,我一直在尝试了解Multithreading
和Socket Programming
.我尝试实现套接字编程,并且在不使用多线程时获得了成功.我对这两个主题都是新手,并且在堆栈本身上提出了2-3个问题,需要在同一方面提供帮助.
For last 48 hours, I have been trying to understand Multithreading
and Socket Programming
. I tried to implement socket programming and had success when not using multithreading. I am new to both of the topics and have raised 2-3 question on stack itself needing help on the same.
在大量搜索之后,我发现了 文章 解释了Socket Programming
和Multithreading
,但是我对本文仍然有很多疑问,并被本文中的Figure 5
所卡住.
After googling a lot I found an article that explains Socket Programming
and Multithreading
, but I still have a lot of doubts in this article and got stuck at Figure 5
in the article.
private void AcceptConnections()
{
while (true)
{
// Accept a connection
Socket socket = _serverSocket.Accept();
ConnectionInfo connection = new ConnectionInfo();
connection.Socket = socket;
// Create the thread for the receives.
connection.Thread = new Thread(ProcessConnection);
connection.Thread.IsBackground = true;
connection.Thread.Start(connection);
// Store the socket
lock (_connections) _connections.Add(connection);
}
}
在最后一行中,您可以看到已使用lock
,并且绑定了delegate ProcessConnection
上方的3-4行.
In the very last line you can see a lock
has been taken and 3-4 lines above a delegate ProcessConnection
is bound.
目前,我尚不清楚此锁的工作方式.锁被拿走后,幕后发生了什么?为什么作者在这里使用锁?如果不锁,那会发生什么?线程ProcessConnection如何工作?同时发生了什么事情?
At this point, I am not clear how this lock is working. What is happening behind the scenes when the lock has taken? Why did the author use lock here? What would have happened if no lock was taken? How does the thread ProcessConnection work? What things are happening simultaneously?
我对所有这些问题感到困惑
I got confused with all these questions
我知道这里有一系列问题,但是如果您能帮助我理解使用多线程的方法,那将是一个很大的帮助.
I know there is a list of questions here, but it would be a great help if you could assist me in understanding the methodology of working with multithreading.
推荐答案
connection.Thread.Start(connection)
通过调用ProcessConnection
启动新线程,并将connection
作为state
参数传递.在新线程中执行ProcessConnection
时,当前线程的执行将立即从下一行继续.
connection.Thread.Start(connection)
starts a new thread with a call to ProcessConnection
, passing connection
as the state
argument. Execution in the current thread continues immediately with the next line while ProcessConnection
is executed in the new thread.
ProcessConnection
从AcceptConnections
传递给它的ConnectionInfo
对象中获取Socket
对象,并等待从套接字接收数据.接收数据时,它将循环遍历connections
集合中的所有其他ConnectionInfo
对象,并将此数据依次发送给每个对象.
ProcessConnection
gets the Socket
object from the ConnectionInfo
object passed to it by AcceptConnections
and waits to receive data from the socket. When it receives data, it loops through all of the other ConnectionInfo
objects in the connections
collection and sends this data to each of them in sequence.
那么,这里正在同时运行什么?好吧,我们有一个初始线程(称为线程0)在无限循环中执行AcceptConnections
.然后对于我们接受的每个套接字连接,我们都有一个线程执行ProcessConnection
.
So what's running concurrently here? Well, we have the initial thread (call it Thread 0) executing AcceptConnections
in an endless loop. And then for each socket connection that we've accepted, we have a thread executing ProcessConnection
.
之所以需要这些锁,是因为ProcessConnection
使用foreach
遍历已知连接来发送数据.如果在foreach
中枚举集合时,线程0将一个新的连接添加到该集合,则在ProcessConnection
中将抛出一个InvalidOperationException
.
The locks are needed because ProcessConnection
uses foreach
to loop through the known connections to send them data. If Thread 0 were to add a new connection to the collection while the collection is being enumerated in the foreach
, an InvalidOperationException
would be thrown in ProcessConnection
.
在这种情况下,lock
确实可以防止并发问题,但是它也会导致潜在的性能问题.它不仅防止AcceptConnections
在枚举ProcessConnection
时修改集合.它还可以防止执行ProcessConnection
的任何两个线程同时枚举集合.在这种情况下,更好的选择是 ReaderWriterLockSlim
允许多个线程同时读取集合.
The lock
does prevent the concurrency issue in this case, but it also causes a potential performance problem. It doesn't only prevent AcceptConnections
from modifying the collection while ProcessConnection
is enumerating it. It also prevents any two threads executing ProcessConnection
from enumerating the collection at the same time. A better choice in this case would be a ReaderWriterLockSlim
which would allow multiple threads to read the collection concurrently.
这篇关于如何使用多线程,锁,套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!