Thread Locking In Static Methods - How?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • McFly Racing

    Thread Locking In Static Methods - How?

    Thread Locking In Static Methods



    I have the need for a Log Manger class that has static methods. Normally I
    would use the lock statement or a Monitor statement both of which take a
    reference to (this). In the case of these static methods I am not able to
    do that.



    How should I perform thread locking within static methods like this? Thank
    you.


  • ShaneB

    #2
    Re: Thread Locking In Static Methods - How?

    public static void LogText(string text)
    {
    lock (typeof(ThisCla ssType))
    {
    ...
    }
    }

    ShaneB

    "McFly Racing" <NoSpam@Microso ft.com> wrote in message
    news:ed2F8XyyEH A.2568@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Thread Locking In Static Methods
    >
    >
    >
    > I have the need for a Log Manger class that has static methods. Normally
    > I
    > would use the lock statement or a Monitor statement both of which take a
    > reference to (this). In the case of these static methods I am not able to
    > do that.
    >
    >
    >
    > How should I perform thread locking within static methods like this?
    > Thank
    > you.
    >
    >[/color]


    Comment

    • McFly Racing

      #3
      Re: Thread Locking In Static Methods - How?

      If I do it this way it seems like I will be loking the whole class instead
      of just this method, is that correct? Thank you.

      "ShaneB" <stormfire1@yah oo.com> wrote in message
      news:OR$PReyyEH A.1564@TK2MSFTN GP09.phx.gbl...[color=blue]
      > public static void LogText(string text)
      > {
      > lock (typeof(ThisCla ssType))
      > {
      > ...
      > }
      > }
      >
      > ShaneB
      >
      > "McFly Racing" <NoSpam@Microso ft.com> wrote in message
      > news:ed2F8XyyEH A.2568@TK2MSFTN GP10.phx.gbl...[color=green]
      > > Thread Locking In Static Methods
      > >
      > >
      > >
      > > I have the need for a Log Manger class that has static methods.[/color][/color]
      Normally[color=blue][color=green]
      > > I
      > > would use the lock statement or a Monitor statement both of which take a
      > > reference to (this). In the case of these static methods I am not able[/color][/color]
      to[color=blue][color=green]
      > > do that.
      > >
      > >
      > >
      > > How should I perform thread locking within static methods like this?
      > > Thank
      > > you.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • John M Deal

        #4
        Re: Thread Locking In Static Methods - How?

        No. What this does is cause a lock of any code location that uses the
        same lock definition. If every method in the class had its code
        encompassed by the same statement it would cause the whole class to lock
        anytime one of the methods was entered, but only if that was the case.

        If you want to make sure that only the one method is locked you could
        create a private static member variable for the specific method, then
        lock against that. For example:

        private static string _logTextLock = "This is used to lock.";
        public static void LogText(string text)
        {
        lock (_logTextLock)
        {
        ...
        }
        }

        Hope that helps.

        Have A Better One!

        John M Deal, MCP
        Necessity Software

        McFly Racing wrote:[color=blue]
        > If I do it this way it seems like I will be loking the whole class instead
        > of just this method, is that correct? Thank you.
        >
        > "ShaneB" <stormfire1@yah oo.com> wrote in message
        > news:OR$PReyyEH A.1564@TK2MSFTN GP09.phx.gbl...
        >[color=green]
        >>public static void LogText(string text)
        >>{
        >> lock (typeof(ThisCla ssType))
        >> {
        >> ...
        >> }
        >>}
        >>
        >>ShaneB
        >>
        >>"McFly Racing" <NoSpam@Microso ft.com> wrote in message
        >>news:ed2F8Xyy EHA.2568@TK2MSF TNGP10.phx.gbl. ..
        >>[color=darkred]
        >>>Thread Locking In Static Methods
        >>>
        >>>
        >>>
        >>>I have the need for a Log Manger class that has static methods.[/color][/color]
        >
        > Normally
        >[color=green][color=darkred]
        >>>I
        >>>would use the lock statement or a Monitor statement both of which take a
        >>>reference to (this). In the case of these static methods I am not able[/color][/color]
        >
        > to
        >[color=green][color=darkred]
        >>>do that.
        >>>
        >>>
        >>>
        >>>How should I perform thread locking within static methods like this?
        >>>Thank
        >>>you.
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]

        Comment

        • ShaneB

          #5
          Re: Thread Locking In Static Methods - How?

          That is correct and after a bit of research, it looks like it's also a bad
          idea:



          What specifially are you doing in your method?

          ShaneB


          "McFly Racing" <NoSpam@Microso ft.com> wrote in message
          news:%231LxJhyy EHA.2876@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > If I do it this way it seems like I will be loking the whole class instead
          > of just this method, is that correct? Thank you.
          >
          > "ShaneB" <stormfire1@yah oo.com> wrote in message
          > news:OR$PReyyEH A.1564@TK2MSFTN GP09.phx.gbl...[color=green]
          >> public static void LogText(string text)
          >> {
          >> lock (typeof(ThisCla ssType))
          >> {
          >> ...
          >> }
          >> }
          >>
          >> ShaneB
          >>
          >> "McFly Racing" <NoSpam@Microso ft.com> wrote in message
          >> news:ed2F8XyyEH A.2568@TK2MSFTN GP10.phx.gbl...[color=darkred]
          >> > Thread Locking In Static Methods
          >> >
          >> >
          >> >
          >> > I have the need for a Log Manger class that has static methods.[/color][/color]
          > Normally[color=green][color=darkred]
          >> > I
          >> > would use the lock statement or a Monitor statement both of which take
          >> > a
          >> > reference to (this). In the case of these static methods I am not able[/color][/color]
          > to[color=green][color=darkred]
          >> > do that.
          >> >
          >> >
          >> >
          >> > How should I perform thread locking within static methods like this?
          >> > Thank
          >> > you.
          >> >
          >> >[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • McFly Racing

            #6
            Re: Thread Locking In Static Methods - How?

            Thanks Shane,

            There are about a dozen or so methods in an existing class I am cleaning up.
            I will set up a few tests and see what I can come up with. I will post my
            solution here when finished. Thanks again.

            "ShaneB" <stormfire1@yah oo.com> wrote in message
            news:O79DtuyyEH A.2896@TK2MSFTN GP10.phx.gbl...[color=blue]
            > That is correct and after a bit of research, it looks like it's also a bad
            > idea:
            >
            >[/color]
            http://msdn.microsoft.com/library/de...ui06032003.asp[color=blue]
            >
            > What specifially are you doing in your method?
            >
            > ShaneB
            >
            >
            > "McFly Racing" <NoSpam@Microso ft.com> wrote in message
            > news:%231LxJhyy EHA.2876@TK2MSF TNGP12.phx.gbl. ..[color=green]
            > > If I do it this way it seems like I will be loking the whole class[/color][/color]
            instead[color=blue][color=green]
            > > of just this method, is that correct? Thank you.
            > >
            > > "ShaneB" <stormfire1@yah oo.com> wrote in message
            > > news:OR$PReyyEH A.1564@TK2MSFTN GP09.phx.gbl...[color=darkred]
            > >> public static void LogText(string text)
            > >> {
            > >> lock (typeof(ThisCla ssType))
            > >> {
            > >> ...
            > >> }
            > >> }
            > >>
            > >> ShaneB
            > >>
            > >> "McFly Racing" <NoSpam@Microso ft.com> wrote in message
            > >> news:ed2F8XyyEH A.2568@TK2MSFTN GP10.phx.gbl...
            > >> > Thread Locking In Static Methods
            > >> >
            > >> >
            > >> >
            > >> > I have the need for a Log Manger class that has static methods.[/color]
            > > Normally[color=darkred]
            > >> > I
            > >> > would use the lock statement or a Monitor statement both of which[/color][/color][/color]
            take[color=blue][color=green][color=darkred]
            > >> > a
            > >> > reference to (this). In the case of these static methods I am not[/color][/color][/color]
            able[color=blue][color=green]
            > > to[color=darkred]
            > >> > do that.
            > >> >
            > >> >
            > >> >
            > >> > How should I perform thread locking within static methods like this?
            > >> > Thank
            > >> > you.
            > >> >
            > >> >
            > >>
            > >>[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Richard Blewett [DevelopMentor]

              #7
              Re: Thread Locking In Static Methods - How?

              Yes, the lock keyword is a terribly named keyword



              However, I'm not sure about your implementation. Three things:

              1. Its a bit heavyweight to allocate a string just for locking
              2. Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned
              3. If anyone updates the string they will end up with a different object and so you then have a race condition

              Its much simpler to use

              private static readonly object myGuardObject = new object();

              Regards

              Richard Blewett - DevelopMentor



              No. What this does is cause a lock of any code location that uses the
              same lock definition. If every method in the class had its code
              encompassed by the same statement it would cause the whole class to lock
              anytime one of the methods was entered, but only if that was the case.

              If you want to make sure that only the one method is locked you could
              create a private static member variable for the specific method, then
              lock against that. For example:

              private static string _logTextLock = "This is used to lock.";
              public static void LogText(string text)
              {
              lock (_logTextLock)
              {
              ...
              }
              }

              Hope that helps.

              Have A Better One!

              John M Deal, MCP
              Necessity Software



              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Thread Locking In Static Methods - How?

                ShaneB <stormfire1@yah oo.com> wrote:[color=blue]
                > public static void LogText(string text)
                > {
                > lock (typeof(ThisCla ssType))
                > {
                > ...
                > }
                > }[/color]

                Both lock(this) and lock(typeof(Thi sClassType)) are a bad idea, IMO.

                See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • John M Deal

                  #9
                  Re: Thread Locking In Static Methods - How?

                  Great catch. The funny thing is that I've actually implemented it in
                  code the way you show it and not the way I did. Some time ago I had read
                  Jeff Richter's article
                  (http://msdn.microsoft.com/msdnmag/issues/03/01/NET/) on this topic and
                  I use it as a reference anytime I need it.

                  I suppose where I screwed up was by responding from memory rather than
                  being sure to check the real thing. Thanks for calling me on it though,
                  it'll help remind me to check my references (and code) before passing on
                  what could turn out to be detrimental information to others.

                  Have A Better One!

                  John M Deal, MCP
                  Necessity Software

                  Richard Blewett [DevelopMentor] wrote:[color=blue]
                  > Yes, the lock keyword is a terribly named keyword
                  >
                  > http://www.dotnetconsult.co.uk/weblo...d-a68b98b24457
                  >
                  > However, I'm not sure about your implementation. Three things:
                  >
                  > 1. Its a bit heavyweight to allocate a string just for locking
                  > 2. Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned
                  > 3. If anyone updates the string they will end up with a different object and so you then have a race condition
                  >
                  > Its much simpler to use
                  >
                  > private static readonly object myGuardObject = new object();
                  >
                  > Regards
                  >
                  > Richard Blewett - DevelopMentor
                  > http://www.dotnetconsult.co.uk/weblog
                  > http://www.dotnetconsult.co.uk
                  >
                  > No. What this does is cause a lock of any code location that uses the
                  > same lock definition. If every method in the class had its code
                  > encompassed by the same statement it would cause the whole class to lock
                  > anytime one of the methods was entered, but only if that was the case.
                  >
                  > If you want to make sure that only the one method is locked you could
                  > create a private static member variable for the specific method, then
                  > lock against that. For example:
                  >
                  > private static string _logTextLock = "This is used to lock.";
                  > public static void LogText(string text)
                  > {
                  > lock (_logTextLock)
                  > {
                  > ...
                  > }
                  > }
                  >
                  > Hope that helps.
                  >
                  > Have A Better One!
                  >
                  > John M Deal, MCP
                  > Necessity Software
                  >
                  >
                  >[/color]

                  Comment

                  • TT (Tom Tempelaere)

                    #10
                    RE: Thread Locking In Static Methods - How?

                    Hi,

                    What you need is a locking object. In this case, it would be a static member
                    of the class. Construct the locking object in the static constructor of the
                    class.

                    Then, in each static method, lock on the locking object.

                    The locking object need not be anything special, just an Object instance
                    will do.

                    Other people suggested locking on the type of the class, which is not a good
                    way to do things in any event (Check Skeet's threading pages for more
                    explanation).

                    HTH,
                    TT

                    "McFly Racing" wrote:
                    [color=blue]
                    > Thread Locking In Static Methods
                    >
                    >
                    >
                    > I have the need for a Log Manger class that has static methods. Normally I
                    > would use the lock statement or a Monitor statement both of which take a
                    > reference to (this). In the case of these static methods I am not able to
                    > do that.
                    >
                    >
                    >
                    > How should I perform thread locking within static methods like this? Thank
                    > you.
                    >
                    >
                    >[/color]

                    Comment

                    • ShaneB

                      #11
                      Re: Thread Locking In Static Methods - How?

                      Thanks for the link. Interesting reading.

                      ShaneB

                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      news:MPG.1c02f6 ad4fefb41198b8d e@msnews.micros oft.com...[color=blue]
                      > ShaneB <stormfire1@yah oo.com> wrote:[color=green]
                      >> public static void LogText(string text)
                      >> {
                      >> lock (typeof(ThisCla ssType))
                      >> {
                      >> ...
                      >> }
                      >> }[/color]
                      >
                      > Both lock(this) and lock(typeof(Thi sClassType)) are a bad idea, IMO.
                      >
                      > See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml
                      >
                      > --
                      > Jon Skeet - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet
                      > If replying to the group, please do not mail me too[/color]


                      Comment

                      Working...