Hi William,
From the IL code you can get some image of what I mean, but acutally, the
IL code also is not the most basic code, it also can be parse into several
instructions.
So you can just regard the IL code as the virtual instructions for
understanding usage.
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "William Stacey" <
[email protected]>
| References: <
[email protected]>
<#
[email protected]>
<#
[email protected]>
<
[email protected]>
| Subject: Re: thread context switch question
| Date: Thu, 9 Oct 2003 01:00:06 -0400
| Lines: 120
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 66.188.59.114.bay.mi.chartermi.net 66.188.59.114
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:190105
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Jeffrey, I think I see the issue with some more digging using the
| following c# code and IL.
| The following C# code translates to the below IL. I guess a context
switch
| could happen between L_0000 and L0007. Is that how you see it?
|
| public void TestLock()
| {
| lock(syncLock)
| {
| }
| }
|
|
| public void TestLock();
|
| .maxstack 2
| .locals (object V_0)
| .try L_000d to L_000f finally L_000f to L_0016
| L_0000: ldarg.0
| L_0001: ldfld syncLock
| L_0006: dup
| L_0007: stloc.0
| L_0008: call Monitor.Enter
| L_000d: leave.s L_0016
| L_000f: ldloc.0
| L_0010: call Monitor.Exit
| L_0015: endfinally
| L_0016: ret
|
| --
| William Stacey, DNS MVP
|
| | >
| > Hi William,
| >
| > Thread context switch can happens in many situation, so you must be more
| > carefully about it.
| > The thread context switch is based on the machine instruction, so even
one
| > sentence of non-instruction should be locked.
| >
| > For example, if you want to increace an interger as i++ in multi-thread
| > environment, you should use Interlocked.Increment method.
| > This is because the i++ will be parse as more than one instructions.
| >
| > Hope this helps,
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! -
www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "William Stacey" <
[email protected]>
| > | References: <
[email protected]>
| > <#
[email protected]>
| > | Subject: Re: thread context switch question
| > | Date: Wed, 8 Oct 2003 19:56:48 -0400
| > | Lines: 41
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Message-ID: <#
[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 66.188.59.114.bay.mi.chartermi.net 66.188.59.114
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:190047
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Just trying to be defensive and figure out where to subtle errors can
| > | appear.
| > | Thanks.
| > | --
| > | William Stacey, DNS MVP
| > |
| > | "Nicholas Paldino [.NET/C# MVP]" <
[email protected]>
| > wrote
| > | in message | > | > William,
| > | >
| > | > Yes, I believe it is. What exactly are you trying to do?
| > | >
| > | > --
| > | > - Nicholas Paldino [.NET/C# MVP]
| > | > - nick(dot)paldino=at=exisconsulting<dot>com
| > | >
| > | > | > | > > Using the following code sample:
| > | > > public byte[] Get()
| > | > > {
| > | > > // <= Possible to switch Here??
| > | > > lock(syncLock)
| > | > > {
| > | > > //Do something in Get().
| > | > > }
| > | > > }
| > | > >
| > | > > It is possible for a thread switch to happen after a method is
| > entered,
| > | > but
| > | > > before the first line of code in the method? I am thinking it is.
| > tia
| > | > >
| > | > > --
| > | > > William Stacey, DNS MVP
| > | > >
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|