At 02:44 PM 06/21/2000 -0400, Dave Harvey wrote:
> Globals within DLL’s are visible to all threads within a given process.
> Locals (stack-based) are specific to each thread. What about locals with the
> ‘static’ keyword? Are they persistent, yet still specific to each thread? Or
> are they treated as globals, with a single such variable visible to all
threads?
They are globals.
There is a declspec to declare globals as “thread local” also, but this
DOES NOT
WORK IN DLLs. There’s a KB article on this somewhere, but the gist is that the
main program allocates the thread local storage prior to knowing anything
about the
DLLs.
I noticed that warning, and thus I’m avoiding the declspec approach.
To be more complete, I’m working on a COM server written using ATL in VC6.
The intent is for this DLL to be used from IIS, which means it will be
accessed by IIS’s thread pool. Each time the DLL is to be used, the worker
thread must set a few properties and then invoke a method.
If multiple IIS worker threads are accessing the DLL in parallel, I’m
concerned that as each IIS worker thread sets various properties they will
be stepping all over each other’s values. So I need to make the property
storage specific to each worker thread. The properties must be stored in
persistent (non-stack-based) storage since there are multiple properties to
be set before the method call. But making them global means all threads in
the process will share a single, common copy of each property.
Reviewing MSDN’s comments on that topic, it appears that I may be able to
avoid this nightmare by proper selection of the COM threading model. The
docs suggest that the server can enforce the threading model of its choice,
and if the caller isn’t compliant COM will “magically” morph the call into
the proper threading model.
The terminology of COM and VC’s ATL wizard differ somewhat, so it’s unclear
which model (if any) will resolve this problem. I believe what I need to do
is cause each thread to run in its own “apartment”, with its own set of
globals within that apartment. Is that what VC’s ATL wizard calls the
“single” threading model? The “apartment” threading model?
As gross as this sounds: Would creating this COM server as an EXE resolve
the problem, by forcing a fresh copy of the EXE (a new process) to be
created for each IIS worker thread that needed such a server? I doubt it,
but someone here suggested that might be the case. I believe such an
out-of-process server would attempt to service all requests for that kind of
server, and we’d be right back to the same problem PLUS the overhead of
communicating across process boundaries.
Thanks!
RLH