Groups | Blog | Home
all groups > inetserver asp components > february 2004 >

inetserver asp components : apartmant thread safety


Pat [MSFT]
2/16/2004 8:47:21 AM
It's not actually an IIS thing. It's how COM threading works. Inside COM
goes over this in detail.

Calls to a specific Single Threaded Apartment (i.e. Apartment 2) are
serialized. But each ASP page runs in a separate COM STA apartment, so you
could have 25 * # CPU's STAs (i.e. 50 on a dual proc). Each will have calls
serialized to it, but they can each run in parallel.

So, in your example, you could have 50
DoMemoryAndProcessIntensiveOperations() going at the same time. Also, COM
only serializes at the interface level. If your function relies on global
variables, then you still must provide some protection for them.

If you want to limit the number of simultaneous calls, you could use a
Semaphore to gate access to 'x' simultaneous threads. The problem then
would be that all other ASP pages that attempt to call will go into a WAIT
until a Semaphore slot becomes available. Another option (I don't recommend
this, but some have done it this way) is to mark the component as Single
Threaded (instead of Apartment). This will cause only one instance to be
active at one time. However, this is most likely going to cause some pretty
severe scaling problems.

Another solution for highly intensive operations w/high latencies is to
decouple the call via MSMQ. Basically the page would post the call into the
queue and return immediately to the user. Then have the page refresh a few
seconds later to see if the operation had completed. Then you could control
the number of simultaneous calls yourself.

Pat

[quoted text, click to view]

Serve Laurijssen
2/16/2004 3:34:33 PM
I'm rather confused about this whole threading business of IIS, ASP and COM
components.
We have an apartment threaded component with a function that is very
resource intensive. One call takes about three seconds and 10mb of memory.
Consider the following code:

STDMETHODIMP SomeTest::Test(BSTR word)
{
DoMemoryAndProcessIntensiveOperation();
return S_OK;
}

I was under the impression that IIS serializes calls to the component, so
that all memory is released before the next call comes in, but it looks like
it is possible for IIS to have more than one call executing at the same
time. Is that correct? What happens if client requests are coming in faster
than it can handle?

Is there some info on this?

AddThis Social Bookmark Button