Reputation: 13195
I would like to use the C# asynchronous io model for my socket. I have multiple threads that need to send over the socket. What is the best way of handling this? A pool of N sockets,with access controlled by lock? Or is asynch send thread-safe for multiple threads accessing a single socket?
THanks!
Jacko
Upvotes: 2
Views: 1482
Reputation: 8885
The async methods already create new threads to send the data. This will probably add unnecessary overhead to your application. If you already have multiple threads, you can create an IDispoable type of object to represent access to the socket and a manager which will control the checkin and checkout for the socket. The socket would checkin itself when the IDisposable dispose method is called. This way you can control what methods your threads can perform on the socket as well. If the socket is already checked out by another thread, the manager would simply block until it's available.
using (SharedSocket socket = SocketManager.GetSocket())
{
//do things with socket
}
Upvotes: 2
Reputation:
System.Threading.Semaphore
is something that becomes handy in synchronizing and racing conditions
Upvotes: 1