By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,723 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,723 IT Pros & Developers. It's quick & easy.

Multi-threaded p/invoke

P: n/a
Hi all,

Does anyone know of a way to achieve multi-theaded platform invokes? I have
been taking quite a performance hit using the syncronised attribute:
[MethodImpl(MethodImplOptions.Synchronized)].

Cheers
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Duncan,

Are you attributing the actual function delcaration with MethodImpl? If
so, then this is probably a bad idea, since it will lock on the type, and if
you have multiple method definitions on the same type, they will all lock on
the same thing (when it probably isn't required).

Are you sure that your library functions are not re-entrant? Are you
sure you have to do this?

If so, then what you should do is create an object (new object()) for
each function that you declare. Declare them as private, and then write
static wrappers around them. In the static wrapper, lock on the object that
corresponds to the method that you are calling through the P/Invoke layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in message
news:Yx*****************@newsfe3-gui.ntli.net...
Hi all,

Does anyone know of a way to achieve multi-theaded platform invokes? I
have
been taking quite a performance hit using the syncronised attribute:
[MethodImpl(MethodImplOptions.Synchronized)].

Cheers
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004

Nov 16 '05 #2

P: n/a
Hi Nicholas,

Thanks for the quick response. I am only assuming that they are not
re-entrant because I was getting exceptions being thrown once I have moved
into mutli-threaded processesing that weren't there in the single threaded
versions: "Object not set to an instance of an object"; I am still in the
process of figuring out why this is. I am thinking maybe its my interop
complaining not the dll - but if I must declare pinvokes as static extern,
how can I avoid it?

I can see how your solution benefits me in that I lock on a single method
rather than the type that owns it however I am working with an encoding dll
that will bottleneck on one particualar method so the improvement will not
be that marked.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uK**************@TK2MSFTNGP12.phx.gbl...
Duncan,

Are you attributing the actual function delcaration with MethodImpl? If so, then this is probably a bad idea, since it will lock on the type, and if you have multiple method definitions on the same type, they will all lock on the same thing (when it probably isn't required).

Are you sure that your library functions are not re-entrant? Are you
sure you have to do this?

If so, then what you should do is create an object (new object()) for
each function that you declare. Declare them as private, and then write
static wrappers around them. In the static wrapper, lock on the object that corresponds to the method that you are calling through the P/Invoke layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in message
news:Yx*****************@newsfe3-gui.ntli.net...
Hi all,

Does anyone know of a way to achieve multi-theaded platform invokes? I
have
been taking quite a performance hit using the syncronised attribute:
[MethodImpl(MethodImplOptions.Synchronized)].

Cheers
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
Nov 16 '05 #3

P: n/a
Duncan,

If the bottleneck will be on one particular method, then how is it the
synchronization of the calls to the DLL functions which is causing the
bottleneck? Based on what you said, it would seem this is not the case, and
that in fact, something else is amiss.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in message
news:rX****************@newsfe3-gui.ntli.net...
Hi Nicholas,

Thanks for the quick response. I am only assuming that they are not
re-entrant because I was getting exceptions being thrown once I have moved
into mutli-threaded processesing that weren't there in the single threaded
versions: "Object not set to an instance of an object"; I am still in the
process of figuring out why this is. I am thinking maybe its my interop
complaining not the dll - but if I must declare pinvokes as static extern,
how can I avoid it?

I can see how your solution benefits me in that I lock on a single method
rather than the type that owns it however I am working with an encoding
dll
that will bottleneck on one particualar method so the improvement will not
be that marked.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:uK**************@TK2MSFTNGP12.phx.gbl...
Duncan,

Are you attributing the actual function delcaration with MethodImpl?

If
so, then this is probably a bad idea, since it will lock on the type, and

if
you have multiple method definitions on the same type, they will all lock

on
the same thing (when it probably isn't required).

Are you sure that your library functions are not re-entrant? Are you
sure you have to do this?

If so, then what you should do is create an object (new object()) for
each function that you declare. Declare them as private, and then write
static wrappers around them. In the static wrapper, lock on the object

that
corresponds to the method that you are calling through the P/Invoke
layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in
message
news:Yx*****************@newsfe3-gui.ntli.net...
> Hi all,
>
> Does anyone know of a way to achieve multi-theaded platform invokes? I
> have
> been taking quite a performance hit using the syncronised attribute:
> [MethodImpl(MethodImplOptions.Synchronized)].
>
> Cheers
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
>
>


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004

Nov 16 '05 #4

P: n/a
Yeah, thats what I was *slowly* figuring. Can you see a problem with the
following

public static uint EncodeChunk(uint hbeStream, byte[] buffer, int index,
uint nBytes, byte[] pOutput, ref uint pdwOutput)

{

uint res = 0;

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

try

{

IntPtr ptr = (IntPtr)(handle.AddrOfPinnedObject().ToInt32()+ind ex);

res = beEncodeChunk(hbeStream, nBytes/2/*Samples*/, ptr, pOutput, ref
pdwOutput);

}

catch (Exception e)

{

MessageBox.Show(e.Message);

}

finally

{

handle.Free();

}

return res;

}


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Duncan,

If the bottleneck will be on one particular method, then how is it the
synchronization of the calls to the DLL functions which is causing the
bottleneck? Based on what you said, it would seem this is not the case, and that in fact, something else is amiss.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in message
news:rX****************@newsfe3-gui.ntli.net...
Hi Nicholas,

Thanks for the quick response. I am only assuming that they are not
re-entrant because I was getting exceptions being thrown once I have moved into mutli-threaded processesing that weren't there in the single threaded versions: "Object not set to an instance of an object"; I am still in the process of figuring out why this is. I am thinking maybe its my interop
complaining not the dll - but if I must declare pinvokes as static extern, how can I avoid it?

I can see how your solution benefits me in that I lock on a single method rather than the type that owns it however I am working with an encoding
dll
that will bottleneck on one particualar method so the improvement will not be that marked.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:uK**************@TK2MSFTNGP12.phx.gbl...
Duncan,

Are you attributing the actual function delcaration with MethodImpl?
If
so, then this is probably a bad idea, since it will lock on the type,
and if
you have multiple method definitions on the same type, they will all
lock on
the same thing (when it probably isn't required).

Are you sure that your library functions are not re-entrant? Are

you sure you have to do this?

If so, then what you should do is create an object (new object()) for each function that you declare. Declare them as private, and then write static wrappers around them. In the static wrapper, lock on the object

that
corresponds to the method that you are calling through the P/Invoke
layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in
message
news:Yx*****************@newsfe3-gui.ntli.net...
> Hi all,
>
> Does anyone know of a way to achieve multi-theaded platform invokes? I > have
> been taking quite a performance hit using the syncronised attribute:
> [MethodImpl(MethodImplOptions.Synchronized)].
>
> Cheers
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
>
>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
Nov 16 '05 #5

P: n/a
Duncan,

Why are you pinning the address of the array? There isn't much reason
to do that, the P/Invoke layer will handle all of that for you (unless the
array is being held by the function somewhere else and operated on outside
of the call).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in message
news:zK*****************@newsfe3-gui.ntli.net...
Yeah, thats what I was *slowly* figuring. Can you see a problem with the
following

public static uint EncodeChunk(uint hbeStream, byte[] buffer, int index,
uint nBytes, byte[] pOutput, ref uint pdwOutput)

{

uint res = 0;

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

try

{

IntPtr ptr = (IntPtr)(handle.AddrOfPinnedObject().ToInt32()+ind ex);

res = beEncodeChunk(hbeStream, nBytes/2/*Samples*/, ptr, pOutput, ref
pdwOutput);

}

catch (Exception e)

{

MessageBox.Show(e.Message);

}

finally

{

handle.Free();

}

return res;

}


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Duncan,

If the bottleneck will be on one particular method, then how is it
the
synchronization of the calls to the DLL functions which is causing the
bottleneck? Based on what you said, it would seem this is not the case,

and
that in fact, something else is amiss.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Duncan Mole" <du**************************@ntlworld.com> wrote in
message
news:rX****************@newsfe3-gui.ntli.net...
> Hi Nicholas,
>
> Thanks for the quick response. I am only assuming that they are not
> re-entrant because I was getting exceptions being thrown once I have moved > into mutli-threaded processesing that weren't there in the single threaded > versions: "Object not set to an instance of an object"; I am still in the > process of figuring out why this is. I am thinking maybe its my interop
> complaining not the dll - but if I must declare pinvokes as static extern, > how can I avoid it?
>
> I can see how your solution benefits me in that I lock on a single method > rather than the type that owns it however I am working with an encoding
> dll
> that will bottleneck on one particualar method so the improvement will not > be that marked.
>
>
> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
> wrote
> in
> message news:uK**************@TK2MSFTNGP12.phx.gbl...
>> Duncan,
>>
>> Are you attributing the actual function delcaration with MethodImpl? > If
>> so, then this is probably a bad idea, since it will lock on the type, and > if
>> you have multiple method definitions on the same type, they will all lock > on
>> the same thing (when it probably isn't required).
>>
>> Are you sure that your library functions are not re-entrant? Are you >> sure you have to do this?
>>
>> If so, then what you should do is create an object (new object()) for >> each function that you declare. Declare them as private, and then write >> static wrappers around them. In the static wrapper, lock on the
>> object
> that
>> corresponds to the method that you are calling through the P/Invoke
>> layer.
>>
>> Hope this helps.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - mv*@spam.guard.caspershouse.com
>>
>> "Duncan Mole" <du**************************@ntlworld.com> wrote in
>> message
>> news:Yx*****************@newsfe3-gui.ntli.net...
>> > Hi all,
>> >
>> > Does anyone know of a way to achieve multi-theaded platform invokes? I >> > have
>> > been taking quite a performance hit using the syncronised attribute:
>> > [MethodImpl(MethodImplOptions.Synchronized)].
>> >
>> > Cheers
>> >
>> >
>> > ---
>> > Outgoing mail is certified Virus Free.
>> > Checked by AVG anti-virus system (http://www.grisoft.com).
>> > Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
>> >
>> >
>>
>>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004
>
>


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.740 / Virus Database: 494 - Release Date: 16/08/2004

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.