Multi-Threading & Performance Questions

1

1944USA

I am re-architecting a C# application written as a multithreaded
Windows Service and trying to squeeze every bit of performance out of
it.


1) Does the thread that an object is instantiated on have any impact on
its performnce?

Example: if I instantiate object "X" on thread "A" pass a reference
of "X" to Thread "B" and then have "B" run "X" (Exclusively). Does
this impact performance as compared to Thread "B" instantiating object
"X" itself?

2) In the above example would it matter if object "X" was using
Interop?

I'm asking the above questions because we currently have a group of
objects instantiated on thread "A" and then when one of the
"Calculation worker threads" > "B","C","D" etc.. need a particular
object, Thread "A" provides them a reference for there exclusive use.
All the objects contain a reference to a COM object through interop
(not sure if the use of Interop has any impact).


I'm running this on an 8 CPU Windows 2003 Server.

As I add "Calculation worker threads" Throughput Performance increases
up to about 3 Threads after that performance actually starts to
decrease. I'm surprised that on an 8 CPU Server that the "tipping
point" is so low. The "Calculation worker threads" are very
mathematically intensive, is it possible there is "blocking" going on
within the .NET Runtime or Interop? I'm trying to pinpoint the bottle
neck. (There is no IO in the Calculation Threads)

Thanks In Advance!!!
 
N

Nicholas Paldino [.NET/C# MVP]

See inline:
1) Does the thread that an object is instantiated on have any impact on
its performnce?

Example: if I instantiate object "X" on thread "A" pass a reference
of "X" to Thread "B" and then have "B" run "X" (Exclusively). Does
this impact performance as compared to Thread "B" instantiating object
"X" itself?

No, but you do have to be careful of any affinity that an object might
have for a thread, as well as for multiple threads hitting the object. You
will need to synchronize access.
2) In the above example would it matter if object "X" was using
Interop?

By interop, I assume you mean COM interop. This is absolutely
important. Objects in COM live in apartments, and you have to set the
apartment state of the thread before you use a COM object. Also, if the
apartment state of the thread is different from that of the object, then a
proxy needs to be created and the object accessed through that.
I'm asking the above questions because we currently have a group of
objects instantiated on thread "A" and then when one of the
"Calculation worker threads" > "B","C","D" etc.. need a particular
object, Thread "A" provides them a reference for there exclusive use.
All the objects contain a reference to a COM object through interop
(not sure if the use of Interop has any impact).

If you are porting this from an unmanaged application, you should be
familiar with the apartment issues already. This will not be too efficient
if the objects are Single-Threaded Apartment (STA) objects. In effect, if
you create all the objects on the main thread, you would have to marshal
proxies to the worker threads. The calls would all then be marshaled to the
main thread where they were created, and in effect, all calls would be
serialized.

You don't have this problem so much with the multi threaded apartment,
free threaded apartment, or neutral threaded apartment models. However,
some might require you to set up your thread in a particular manner.

Hope this helps.
 
W

Willy Denoyette [MVP]

|I am re-architecting a C# application written as a multithreaded
| Windows Service and trying to squeeze every bit of performance out of
| it.
|
|
| 1) Does the thread that an object is instantiated on have any impact on
| its performnce?
|
| Example: if I instantiate object "X" on thread "A" pass a reference
| of "X" to Thread "B" and then have "B" run "X" (Exclusively). Does
| this impact performance as compared to Thread "B" instantiating object
| "X" itself?
|
If the object is exclusively used by B, the answer is no.

| 2) In the above example would it matter if object "X" was using
| Interop?
|
| I'm asking the above questions because we currently have a group of
| objects instantiated on thread "A" and then when one of the
| "Calculation worker threads" > "B","C","D" etc.. need a particular
| object, Thread "A" provides them a reference for there exclusive use.
| All the objects contain a reference to a COM object through interop
| (not sure if the use of Interop has any impact).
|
If your COM object is a STA class object, then you have to take care you
aren't creating an instance of these COM objects when creating an instance
of your class. That means you must create the COM object on your secondary
threads.
You also have to make sure your COM objects run in a compatible apartment,
that is, when your COM objects are STA type objects, you have to initialize
your threads to enter an STA (set the ApartmentState property to STA before
starting the threads). Failing to do so will create a huge bottleneck:
- all COM objects will live in the single process STA thread, not in the
creators thread apartment which is MTA per default,
- all calls to the objects are automatically marshaled/synchronized, so only
a single call can be handled at a time.

|
| I'm running this on an 8 CPU Windows 2003 Server.
|
| As I add "Calculation worker threads" Throughput Performance increases
| up to about 3 Threads after that performance actually starts to
| decrease. I'm surprised that on an 8 CPU Server that the "tipping
| point" is so low. The "Calculation worker threads" are very
| mathematically intensive, is it possible there is "blocking" going on
| within the .NET Runtime or Interop? I'm trying to pinpoint the bottle
| neck. (There is no IO in the Calculation Threads)
|

Looks like your COM objects are STA and are living in a single STA threads
apartment. That means you have to initialize your threads to enter a STA
(see Thread.ApartmentState) and make sure you create the COM instance in
this same thread.



Willy.
 

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