Async sockets and threads

N

nyhetsgrupper

Hi,
I've written a async server app. This app start by connecting to a
client and then send some data (BeginSend). When the data is sent, the
server is starting to listen for incomming data. (BeginRecieve). In the
receive callback I always call BeginRecieve again to keep listen for
more data. The server is continualy listening, and from time to time
sending (but if it is sending more data when there is already a
listening socket, I don't run BeginReceive again). I have one socket
for each client. This works very well but I am very confused about the
Thread-count showing in Windows-task-manager. I would expect that if
the server is Connected to 60 clients, it would also be 60 threads,
because I am listening for incomme all the time. What suprise me is
that the thread count is about 30?? How come? Can anybody explain?
 
G

Guest

BeginRecieve on a socket wont occupy a workerthread but will use an IO
Completion Thread in the OS.

Ciaran O'Donnell
 
W

William Stacey [MVP]

Just to clarify for others. Techinically, BeginReceive uses the caller's
thread until it returns IAsyncResult. When the IO completes, the callback
is called on an IOCP thread. Until then, no threads are used.

--
William Stacey [MVP]

message | BeginRecieve on a socket wont occupy a workerthread but will use an IO
| Completion Thread in the OS.
|
| Ciaran O'Donnell
|
| "(e-mail address removed)" wrote:
|
| > Hi,
| > I've written a async server app. This app start by connecting to a
| > client and then send some data (BeginSend). When the data is sent, the
| > server is starting to listen for incomming data. (BeginRecieve). In the
| > receive callback I always call BeginRecieve again to keep listen for
| > more data. The server is continualy listening, and from time to time
| > sending (but if it is sending more data when there is already a
| > listening socket, I don't run BeginReceive again). I have one socket
| > for each client. This works very well but I am very confused about the
| > Thread-count showing in Windows-task-manager. I would expect that if
| > the server is Connected to 60 clients, it would also be 60 threads,
| > because I am listening for incomme all the time. What suprise me is
| > that the thread count is about 30?? How come? Can anybody explain?
| >
| >
 
C

Chris Mullins

I've written a async server app.
[...]

I have one socket
for each client. This works very well but I am very confused
about the Thread-count showing in Windows-task-manager.
I would expect that if the server is Connected to 60 clients, it
would also be 60 threads, because I am listening for incomme
all the time. What suprise me is that the thread count is
about 30?? How come? Can anybody explain?

When you use Async Sockets, you're not using a 1-thread-per-socket model at
all. You're posting events to I/O Completion Ports which in turn is playing
some very advanced threading games under the hood.

By your reasoning, if I had 1000 users connected I would have 1000 threads.
100000 users would be 100000 threads. The scaling of this is pretty brutal
and probably wouldn't get too far past 1000 threads, at least not with any
semblance of performance.

Windows Async Sockets has crazy performance and scalability and this is
mostly because it's not using the 1-thread-per-socket approach. For some
more information on this approach and some variations, see my blog:
http://www.coversant.net/dotnetnuke/Default.aspx?tabid=88&EntryID=10
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top