Question on threading and using P/Invoke...  
Author Message
Trax





PostPosted: Wed Aug 18 16:11:31 CDT 2004 Top

Visual C#.Net >> Question on threading and using P/Invoke... If a DLL has been created to be thread-safe, and you create a wrapper
class around it in C# that is non-static (i.e. you must instantiate
objects from it), what are the implications of creating multiple objects
from said wrapper class? There's only one DLL, so is it creating
multiple instances of its functions when each object is making calls to
it? Does anyone know of any resources that explain this? Thanks, gilad

DotNet159  
 
 
Nicholas





PostPosted: Wed Aug 18 16:11:31 CDT 2004 Top

Visual C#.Net >> Question on threading and using P/Invoke... giliad,

There is no implication. Your class is going to be a wrapper around the
calls to the DLL, and you are probably going to pass something like a
handle, which will represent the data the function needs to operate on. The
DLL function will be responsible for handling concurrency. You only have to
worry about concurrency in the wrapper level (locking access to the handle,
for example).

As for multiple instance of the dll, this isn't the case. The dll will
be loaded once for the process, and that's it. Functions are not loaded
every time they are called, rather, they are mapped to the process space
until unloaded. So in this case, the function is loaded once, but is being
called in multiple places.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- EMail@HideDomain.com

<EMail@HideDomain.com> wrote in message
news:%EMail@HideDomain.com...
> If a DLL has been created to be thread-safe, and you create a wrapper
> class around it in C# that is non-static (i.e. you must instantiate
> objects from it), what are the implications of creating multiple objects
> from said wrapper class? There's only one DLL, so is it creating multiple
> instances of its functions when each object is making calls to it? Does
> anyone know of any resources that explain this? Thanks, gilad


 
 
gilad





PostPosted: Wed Aug 18 16:49:24 CDT 2004 Top

Visual C#.Net >> Question on threading and using P/Invoke... Nicholas Paldino [.NET/C# MVP] wrote:
> As for multiple instance of the dll, this isn't the case. The dll will
> be loaded once for the process, and that's it. Functions are not loaded
> every time they are called, rather, they are mapped to the process space
> until unloaded. So in this case, the function is loaded once, but is being
> called in multiple places.

I guess this is where I don't understand it. The DLL is loaded, and if
its function is executing for ObjectA of the wrapper (let's say it's
reading a long file and converting it), and a thread begins to execute
the second object, ObjectB of the wrapper, that calls that same function
of the DLL, what happens at this point? Does ObjectB get blocked until
the call from ObjectA finishes? Or is something else happening?

Sorry if I'm not clear in explaining this. I am using P/Invoke to call a
DLL to do some file conversions, and I want to make sure I've dotted my
i's and crossed my t's so to speak.
 
 
Nicholas





PostPosted: Thu Aug 19 10:20:22 CDT 2004 Top

Visual C#.Net >> Question on threading and using P/Invoke... gilad,

Generally speaking, the functions are self contained (or at least, they
should be). It is possible that the functions access information that is
shared in the DLL, and in that case, the functions inside the dll have to
worry about thread concurrency (or maybe you do, if it is coded badly).
However, if you are not really altering any state that is stored in the DLL,
then you don't need to anything for concurrency, as each function call will
get its own stack space in the separate threads, and only work on variables
in that stack space.


--
- Nicholas Paldino [.NET/C# MVP]
- EMail@HideDomain.com

<EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> Nicholas Paldino [.NET/C# MVP] wrote:
>> As for multiple instance of the dll, this isn't the case. The dll
>> will be loaded once for the process, and that's it. Functions are not
>> loaded every time they are called, rather, they are mapped to the process
>> space until unloaded. So in this case, the function is loaded once, but
>> is being called in multiple places.
>
> I guess this is where I don't understand it. The DLL is loaded, and if its
> function is executing for ObjectA of the wrapper (let's say it's reading a
> long file and converting it), and a thread begins to execute the second
> object, ObjectB of the wrapper, that calls that same function of the DLL,
> what happens at this point? Does ObjectB get blocked until the call from
> ObjectA finishes? Or is something else happening?
>
> Sorry if I'm not clear in explaining this. I am using P/Invoke to call a
> DLL to do some file conversions, and I want to make sure I've dotted my
> i's and crossed my t's so to speak.


 
 
gilad





PostPosted: Thu Aug 19 12:24:58 CDT 2004 Top

Visual C#.Net >> Question on threading and using P/Invoke... Nicholas Paldino [.NET/C# MVP] wrote:
> Generally speaking, the functions are self contained (or at least, they
> should be). It is possible that the functions access information that is
> shared in the DLL, and in that case, the functions inside the dll have to
> worry about thread concurrency (or maybe you do, if it is coded badly).
> However, if you are not really altering any state that is stored in the DLL,
> then you don't need to anything for concurrency, as each function call will
> get its own stack space in the separate threads, and only work on variables
> in that stack space.

I appreciate your help.