Re: IPC and Threading in DLL
"Ezmeralda" wrote:
I have the following application requirements:
I need to have one DLL which may be used by several
processes at the same time. This DLL should communicate
to hardware and make the hardware available to all
the processes connected to the DLL.
I have the following design idea in mind:
- the first process connecting to the DLL starts a
thread in DllMain() (DLL_PROCESS_ATTACH) which
communicates to the hardware and puts data in
a global shared section
- all other processes connecting to the DLL do
not start a thread but can access the data in
the global section
- when the last proccess detaches from the DLL,
the communication-thread should be terminated
Unfortunately, it seems that the communication thread
is already terminated when the process which created
this thread (i. e. the first process, that connected
to the DLL) is terminated. I require the communication
thread to keep running until the very last process
detaches
from the DLL.
The problem is that you're thinking about DLL as independent
entity. DLL is just a piece of executable code mapped into
process' address space. It doesn't have threads or any free
will to do things. Process, on the contrary, does. That's
why any thread created within a process belongs to the
process and will die together with the process.
In your specific case I'd create separate dummy process,
which sole purpose is to load the DLL and initiate
communication thread. Then other processes will communicate
with this dummy process. When last client disconnects, then
dummy process terminates itself.
You have an option to develop process start/stop mechanics
by yourself or, alternatively, use some ready technology
like COM. COM out-of-process server infrastructure already
has all necessary features for your task. However, if you're
not acquaint with COM, then developing such infrastructure
could be faster and simpler for you.
Alex