473,883 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asynchronous Programming

I'm trying to determine if I need to make my application multi-threaded, or
if it can be done with asynchronous programming. I realize that asynch calls
do create a new thread in the background, but when they come back, they
return to the thread that launched them, is that correct? If I go
multi-threaded, then I just need to spawn a new thread for each new
connection, right?

What I'm doing here is making a service that accepts connections via TCP,
does some stuff, and then returns a response. Would my best bet be to go
asych or multi-threaded? Any articles on either or both would be much
appreciated.

Thanks,
Steve
Nov 15 '05
48 5461
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:Oy******** ******@TK2MSFTN GP11.phx.gbl:
Lay it on me brother :-) As a side note, was not talking about Indy, but


Yes. I know - its just that I know all about this threading stuff and common
misconceptions because of having to explain it so many times on the Delphi
forums. :)

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #11
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:u6******** ******@tk2msftn gp13.phx.gbl:
A good way to go would be to wait on multiple socket events (one socket for
each tcp client) in a WaitForMultiple Events call, then get the index of the
The problem with this is there is a limit of 64 per call, so you still need
to break it into threads anways. And then you break your code away from
seqeuntial to handling 64 state machines... makes for ugly code.
contention for shared resources goes up with number of threads.. If many
clients, you probably want to go async. You could probably also use I/O
completion ports and a thread pool, have not really seen good examples of
this from managed code however.


Indy has support for this using fibers. This part is in SuperCore and hasnt
been ported to .net yet. This part will likely have to be unmanaged until
fibers, etc are accesible as managed objects.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #12
"Steve - DND" <steve!@!digita lnothing.com> wrote in
news:ux******** ******@TK2MSFTN GP09.phx.gbl:
One thing I have a question on is scalability performance. I keep
reading that a multi-threaded approach may not necessarily scale very
Key word is "may not". In most cases, espeically with sockets its fine. In
fact its better.
well. How many threads is too many before the switching starts to kill
the app? I will probably have about 50-75(possibly up to 100) concurrent
It depends what they are doing. But just to prove a point, press Ctrl-Alt-Del
now. Select task manager, then Performance tab.

I have 493 threads running, with 1% CPU load. My machine is barely loaded
right now compared to what I normally do to it. And guess what? Ive never had
any smoke roll out of the fan port yet. :)

What you will hit before any limits is the process memory limit. You will hit
this at about 1000 threads (There are ways to go higher, but normally you
should not as 1000 threads in one process has other issues).

If you arent going over 200-300 threads, dont even worry about it. That is
unless each one is calculating Pi to the 4 billionth digit.
connections at peak times. From what I have read, it sounds like this
would be too many threads to create.


Not at all. Windows wont even notice that many.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #13
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:eK******** ******@TK2MSFTN GP09.phx.gbl:
I would agree. If all those clients are ~active, your going to spend a
larger respective amount of time context switching and competing for
locks, etc. then actually working. Each thread also gets a smaller
Not true as people assume.... If they are all active calculating Pi - yes.
But active in this case means "socket activity" then no. The reason for
this is bandwidth.

Lets take a 100 MB ethernet. Calculate its max speed against the CPU. You
will see that it is MANY magnitudes slower than even todays most slothful
of CPUs. Factor in hardward buffers, and software driver buffers and each
thread is activated very rarely in the scheme of things even in a fully
loaded ethernet card.

Beause of this, threads spend most of their time waiting on network calls.
This causes them to be skipped over by the scheduler until their blocking
calls have returned. This reduces the "theoretica l" context switching by
many thousands.
quantum time to do work as more threads are running in the system. So
Most thread in this type of system wont reach their quantum because they
will voluntarily sleep first.
less time each thread has to work, more context switch time, more
Nope - because they will switch to threads which are running, which in such
a system most threads on each pass will be sleeping. In fact many of the
switches will be to the idle or system threads.
contention for locks, etc. I am not sure the Async read and writes
using standard Socket or TCPClient are much better. AFAICT, if you post
There are two types of asyncs. The asyncs with windows messages are usually
even slower.

Another factor is the blocking "thunk" to kernel level. But again, only for
advanced systems.

In the scheme of things, what this user describes is a very small system
and he should focus on clean readable code. Performance will be just fine
and there is not need to build and advanced design and muck up his code.
If perf is key, you may have to resort to overlapped io sockets or
WSAEventSelect and wrap a few pInvoke apis.


Problems there too. :)

IOCP is the only TRUE fast way, and it has a TON of problems that should be
avoided and only used as a LAST resort.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #14
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:Oy******** ******@TK2MSFTN GP11.phx.gbl:
Lay it on me brother :-) As a side note, was not talking about Indy, but


Yes. I know - its just that I know all about this threading stuff and common
misconceptions because of having to explain it so many times on the Delphi
forums. :)

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #15
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:u6******** ******@tk2msftn gp13.phx.gbl:
A good way to go would be to wait on multiple socket events (one socket for
each tcp client) in a WaitForMultiple Events call, then get the index of the
The problem with this is there is a limit of 64 per call, so you still need
to break it into threads anways. And then you break your code away from
seqeuntial to handling 64 state machines... makes for ugly code.
contention for shared resources goes up with number of threads.. If many
clients, you probably want to go async. You could probably also use I/O
completion ports and a thread pool, have not really seen good examples of
this from managed code however.


Indy has support for this using fibers. This part is in SuperCore and hasnt
been ported to .net yet. This part will likely have to be unmanaged until
fibers, etc are accesible as managed objects.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #16
"Steve - DND" <steve!@!digita lnothing.com> wrote in
news:ux******** ******@TK2MSFTN GP09.phx.gbl:
One thing I have a question on is scalability performance. I keep
reading that a multi-threaded approach may not necessarily scale very
Key word is "may not". In most cases, espeically with sockets its fine. In
fact its better.
well. How many threads is too many before the switching starts to kill
the app? I will probably have about 50-75(possibly up to 100) concurrent
It depends what they are doing. But just to prove a point, press Ctrl-Alt-Del
now. Select task manager, then Performance tab.

I have 493 threads running, with 1% CPU load. My machine is barely loaded
right now compared to what I normally do to it. And guess what? Ive never had
any smoke roll out of the fan port yet. :)

What you will hit before any limits is the process memory limit. You will hit
this at about 1000 threads (There are ways to go higher, but normally you
should not as 1000 threads in one process has other issues).

If you arent going over 200-300 threads, dont even worry about it. That is
unless each one is calculating Pi to the 4 billionth digit.
connections at peak times. From what I have read, it sounds like this
would be too many threads to create.


Not at all. Windows wont even notice that many.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #17
"William Stacey [MVP]" <st***********@ mvps.org> wrote in
news:eK******** ******@TK2MSFTN GP09.phx.gbl:
I would agree. If all those clients are ~active, your going to spend a
larger respective amount of time context switching and competing for
locks, etc. then actually working. Each thread also gets a smaller
Not true as people assume.... If they are all active calculating Pi - yes.
But active in this case means "socket activity" then no. The reason for
this is bandwidth.

Lets take a 100 MB ethernet. Calculate its max speed against the CPU. You
will see that it is MANY magnitudes slower than even todays most slothful
of CPUs. Factor in hardward buffers, and software driver buffers and each
thread is activated very rarely in the scheme of things even in a fully
loaded ethernet card.

Beause of this, threads spend most of their time waiting on network calls.
This causes them to be skipped over by the scheduler until their blocking
calls have returned. This reduces the "theoretica l" context switching by
many thousands.
quantum time to do work as more threads are running in the system. So
Most thread in this type of system wont reach their quantum because they
will voluntarily sleep first.
less time each thread has to work, more context switch time, more
Nope - because they will switch to threads which are running, which in such
a system most threads on each pass will be sleeping. In fact many of the
switches will be to the idle or system threads.
contention for locks, etc. I am not sure the Async read and writes
using standard Socket or TCPClient are much better. AFAICT, if you post
There are two types of asyncs. The asyncs with windows messages are usually
even slower.

Another factor is the blocking "thunk" to kernel level. But again, only for
advanced systems.

In the scheme of things, what this user describes is a very small system
and he should focus on clean readable code. Performance will be just fine
and there is not need to build and advanced design and muck up his code.
If perf is key, you may have to resort to overlapped io sockets or
WSAEventSelect and wrap a few pInvoke apis.


Problems there too. :)

IOCP is the only TRUE fast way, and it has a TON of problems that should be
avoided and only used as a LAST resort.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Nov 15 '05 #18
Chad Z. Hower aka Kudzu <cp**@hower.org > wrote:
It depends what they are doing. But just to prove a point, press Ctrl-Alt-Del
now. Select task manager, then Performance tab.

I have 493 threads running, with 1% CPU load. My machine is barely loaded
right now compared to what I normally do to it. And guess what? Ive never had
any smoke roll out of the fan port yet. :)


This metric is fairly meaningless because it doesn't indicate how many
of those threads are active at any given time. If only a handful of
those threads are active, there won't be much context switching going
on.

If all 493 threads are busy reading/writing data to/from reasonably
active socket connections, you'll be doing lots of context switches per
second, and chewing up a lot more CPU doing those context switches.
Nov 15 '05 #19
Chad Z. Hower aka Kudzu <cp**@hower.org > wrote:
It depends what they are doing. But just to prove a point, press Ctrl-Alt-Del
now. Select task manager, then Performance tab.

I have 493 threads running, with 1% CPU load. My machine is barely loaded
right now compared to what I normally do to it. And guess what? Ive never had
any smoke roll out of the fan port yet. :)


This metric is fairly meaningless because it doesn't indicate how many
of those threads are active at any given time. If only a handful of
those threads are active, there won't be much context switching going
on.

If all 493 threads are busy reading/writing data to/from reasonably
active socket connections, you'll be doing lots of context switches per
second, and chewing up a lot more CPU doing those context switches.
Nov 15 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3490
by: ... | last post by:
Does anyone know a good tutorial on asynchronous programming in .net AsyncCallback And IASyncResult are driving me crazy. And the msdn documentation is not really helpful on this topic I appreciate any recommendation.
4
1572
by: Chris | last post by:
Hello, With asynchronous programming : Why does the callback-function (running in a worker thread) may not update the state of a control on the main-form ? The docs say that you must use a MethodInvoker instead as in following example :
9
8680
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should be faster than remoting and is a better choice for what I am doing but that it is difficult to code.
4
348
by: bernardpace | last post by:
Hi, I am trying to get more familiar with asynchronous programming. I was reading through the document found on the page: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp Now, I read that 'Always call EndInvoke after your asynchronous call completes'.
1
1559
by: Julian Hershel | last post by:
Reading about asynchronous programming (ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconasynchronousdesignpatterno verview.htm) I could not clarify some doubts. Hope you can help me. 1) Are asynchronous programming and multithreaded programming two different pictures? As I read the help topic above it is not clear to me if the design pattern opens a new thread or not to run the methods asynchronously. 2) One unique thread can run...
1
3178
by: org | last post by:
Hi, I'm developing a web service with should be used by an .NET CF2 client and an .NET 2.0 Windows client. I've tried to put all the connection logic into one class, which could be used in common from the mobile and the windows client. But this didn't work: The new .NET framework supports asynchronous web services with the "MethodnameCompleted+=new MethodNameEventHandler pattern, but the "old style" .NET 1 pattern used in the compact...
5
2306
by: Ryan Liu | last post by:
Hi, I read Microsoft SDK, ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming overview.htm there are 4 ways to call EndInvoke: The code in this topic demonstrates four common ways to use BeginInvoke and EndInvoke to make asynchronous calls. After calling BeginInvoke you can:
3
2105
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through different stages of execution. However I was wondering if there was any information available regarding the limitations of the asynchronous model, in particular maximum numbers of asynchronous elements, and limitations of multiple requests potentially...
3
1544
by: senfo | last post by:
I recently read an MSDN article by Jeff Prosise titled, Scalable Apps with Asynchronous Programming in ASP.NET (http://msdn.microsoft.com/msdnmag/issues/07/03/WickedCode/). In the article, Jeff discusses scaling problems that some users experience with ASP.NET applications. The problem, Jeff writes, isn't that ASP.NET isn't capable of scaling; but has more to do with ASP.NET applications that use threads inefficiently. In the article,...
2
1464
by: =?Utf-8?B?U3JpcmFtIE1hbGxhanlvc3VsYQ==?= | last post by:
Hi, I was going through the article (http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/) regarding async programming in ASP.Net 2.0 but did not find much of an use with it. Actually my requirement is that once a user logs in I need to call a web service which will perform some activity but I am not bothered with the result of the web service. I have a process.aspx which is called after login and after implementing the code...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7959
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3227
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.