Only static (Shared) types are thread safe ?!

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

    Only static (Shared) types are thread safe ?!

    OleDbCommand class like many .NET classes has the following description in
    its help file:

    "Thread Safety
    Any public static (Shared in Visual Basic) members of this type are safe for
    multithreaded operations. Any instance members are not guaranteed to be
    thread safe."
    I have 2 questions:

    1. I thought dynamic variables are thread-safe since threads have their own
    stack and static data had to be synchronized. However, many many .NET
    classes say ONLY static members of thier type are thread safe. Why is that?


    2. Do we still have to synchronize access to this shared data?

    3. If only static members are thread-safe why all the examples use non
    static data?

    Thanks.
    Dee


  • Scott Allen

    #2
    Re: Only static (Shared) types are thread safe ?!

    There is overhead in making a method thread safe. Microsoft typically
    will not add the overhead to an instance (non static) method, because
    90% of the time the overhead will be unneeded. This leaves it up to
    the developer to add thread safety only when they need it.

    Microsoft will typically make static methods threadsafe, because there
    is a good chance the Type will be used in an MT environment.

    I have a couple articles on the topic, let me know if they help or if
    they can be improved:


    Statics And Thread Safety (Parts I and II)



    --
    Scott


    On Wed, 31 Aug 2005 08:55:09 -0400, "dee" <dee@home.net > wrote:
    [color=blue]
    >OleDbCommand class like many .NET classes has the following description in
    >its help file:
    >
    >"Thread Safety
    >Any public static (Shared in Visual Basic) members of this type are safe for
    >multithreade d operations. Any instance members are not guaranteed to be
    >thread safe."
    >I have 2 questions:
    >
    > 1. I thought dynamic variables are thread-safe since threads have their own
    >stack and static data had to be synchronized. However, many many .NET
    >classes say ONLY static members of thier type are thread safe. Why is that?
    >
    >
    >2. Do we still have to synchronize access to this shared data?
    >
    >3. If only static members are thread-safe why all the examples use non
    >static data?
    >
    >Thanks.
    >Dee
    >[/color]

    Comment

    • Damien

      #3
      Re: Only static (Shared) types are thread safe ?!

      dee wrote:[color=blue]
      > OleDbCommand class like many .NET classes has the following description in
      > its help file:
      >
      > "Thread Safety
      > Any public static (Shared in Visual Basic) members of this type are safe for
      > multithreaded operations. Any instance members are not guaranteed to be
      > thread safe."
      > I have 2 questions:
      >
      > 1. I thought dynamic variables are thread-safe since threads have their own
      > stack and static data had to be synchronized. However, many many .NET
      > classes say ONLY static members of thier type are thread safe. Why is that?
      >
      >
      > 2. Do we still have to synchronize access to this shared data?
      >
      > 3. If only static members are thread-safe why all the examples use non
      > static data?
      >
      > Thanks.
      > Dee[/color]

      I think this is a language problem here. I believe the phrase:

      "Any public static (Shared in Visual Basic) members of this type are
      safe for multithreaded operations"

      is meant to be read

      "Any public static (Shared in Visual Basic) memebers that this type
      exposes are safe for multithreaded operations"

      rather than

      "Any public static (Shared in Visual Basic) members defined to be of
      this type are safe for multithreaded operations"

      It's quite ambiguous phrasing, but I believe that what I have stated
      (the second quote) is the intended meaning.

      Does this clear things up?

      Damien

      Comment

      • dee

        #4
        Re: Only static (Shared) types are thread safe ?!

        Thanks Scott nice articles.
        I guess my problem is with the meaning of the line from help file:
        "Any public static (Shared in Visual Basic) members of this type are safe
        for
        multithreaded operations. Any instance members are not guaranteed to be
        thread safe."

        When they day "member" then mean either a method or a property?
        "instance member" could be a method that uses a static proptery? If so, then
        they should be handled like other static methods since thier user does
        doesnt know how they are implemented.

        Thanks for your time.
        Dee


        "Scott Allen" <scott@nospam.o detocode.com> wrote in message
        news:jmebh110ch pid5379euijdo90 t17pn0rks@4ax.c om...[color=blue]
        > There is overhead in making a method thread safe. Microsoft typically
        > will not add the overhead to an instance (non static) method, because
        > 90% of the time the overhead will be unneeded. This leaves it up to
        > the developer to add thread safety only when they need it.
        >
        > Microsoft will typically make static methods threadsafe, because there
        > is a good chance the Type will be used in an MT environment.
        >
        > I have a couple articles on the topic, let me know if they help or if
        > they can be improved:
        >
        >
        > Statics And Thread Safety (Parts I and II)
        > http://odetocode.com/Articles/313.aspx
        > http://odetocode.com/Articles/314.aspx
        >
        > --
        > Scott
        > http://www.OdeToCode.com/blogs/scott/
        >
        > On Wed, 31 Aug 2005 08:55:09 -0400, "dee" <dee@home.net > wrote:
        >[color=green]
        >>OleDbComman d class like many .NET classes has the following description in
        >>its help file:
        >>
        >>"Thread Safety
        >>Any public static (Shared in Visual Basic) members of this type are safe
        >>for
        >>multithread ed operations. Any instance members are not guaranteed to be
        >>thread safe."
        >>I have 2 questions:
        >>
        >> 1. I thought dynamic variables are thread-safe since threads have their
        >> own
        >>stack and static data had to be synchronized. However, many many .NET
        >>classes say ONLY static members of thier type are thread safe. Why is
        >>that?
        >>
        >>
        >>2. Do we still have to synchronize access to this shared data?
        >>
        >>3. If only static members are thread-safe why all the examples use non
        >>static data?
        >>
        >>Thanks.
        >>Dee
        >>[/color]
        >[/color]


        Comment

        • dee

          #5
          Re: Only static (Shared) types are thread safe ?!

          Yes Damian Thanks.
          In addition, why would an intrinsic method be unsafe? cuz it might access
          static data? Then why not thread safe those as well?


          "Damien" <Damien_The_Unb eliever@hotmail .com> wrote in message
          news:1125500227 .937604.10970@g 43g2000cwa.goog legroups.com...[color=blue]
          > dee wrote:[color=green]
          >> OleDbCommand class like many .NET classes has the following description
          >> in
          >> its help file:
          >>
          >> "Thread Safety
          >> Any public static (Shared in Visual Basic) members of this type are safe
          >> for
          >> multithreaded operations. Any instance members are not guaranteed to be
          >> thread safe."
          >> I have 2 questions:
          >>
          >> 1. I thought dynamic variables are thread-safe since threads have their
          >> own
          >> stack and static data had to be synchronized. However, many many .NET
          >> classes say ONLY static members of thier type are thread safe. Why is
          >> that?
          >>
          >>
          >> 2. Do we still have to synchronize access to this shared data?
          >>
          >> 3. If only static members are thread-safe why all the examples use non
          >> static data?
          >>
          >> Thanks.
          >> Dee[/color]
          >
          > I think this is a language problem here. I believe the phrase:
          >
          > "Any public static (Shared in Visual Basic) members of this type are
          > safe for multithreaded operations"
          >
          > is meant to be read
          >
          > "Any public static (Shared in Visual Basic) memebers that this type
          > exposes are safe for multithreaded operations"
          >
          > rather than
          >
          > "Any public static (Shared in Visual Basic) members defined to be of
          > this type are safe for multithreaded operations"
          >
          > It's quite ambiguous phrasing, but I believe that what I have stated
          > (the second quote) is the intended meaning.
          >
          > Does this clear things up?
          >
          > Damien
          >[/color]


          Comment

          • dee

            #6
            Re: Only static (Shared) types are thread safe ?!

            OleDbCommand class doesnt have any static members anyway
            what is the point of mentioning static members are thread safe ??

            "dee" <dee@home.net > wrote in message
            news:OWaVVZkrFH A.2876@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Yes Damian Thanks.
            > In addition, why would an intrinsic method be unsafe? cuz it might access
            > static data? Then why not thread safe those as well?
            >
            >
            > "Damien" <Damien_The_Unb eliever@hotmail .com> wrote in message
            > news:1125500227 .937604.10970@g 43g2000cwa.goog legroups.com...[color=green]
            >> dee wrote:[color=darkred]
            >>> OleDbCommand class like many .NET classes has the following description
            >>> in
            >>> its help file:
            >>>
            >>> "Thread Safety
            >>> Any public static (Shared in Visual Basic) members of this type are safe
            >>> for
            >>> multithreaded operations. Any instance members are not guaranteed to be
            >>> thread safe."
            >>> I have 2 questions:
            >>>
            >>> 1. I thought dynamic variables are thread-safe since threads have their
            >>> own
            >>> stack and static data had to be synchronized. However, many many .NET
            >>> classes say ONLY static members of thier type are thread safe. Why is
            >>> that?
            >>>
            >>>
            >>> 2. Do we still have to synchronize access to this shared data?
            >>>
            >>> 3. If only static members are thread-safe why all the examples use non
            >>> static data?
            >>>
            >>> Thanks.
            >>> Dee[/color]
            >>
            >> I think this is a language problem here. I believe the phrase:
            >>
            >> "Any public static (Shared in Visual Basic) members of this type are
            >> safe for multithreaded operations"
            >>
            >> is meant to be read
            >>
            >> "Any public static (Shared in Visual Basic) memebers that this type
            >> exposes are safe for multithreaded operations"
            >>
            >> rather than
            >>
            >> "Any public static (Shared in Visual Basic) members defined to be of
            >> this type are safe for multithreaded operations"
            >>
            >> It's quite ambiguous phrasing, but I believe that what I have stated
            >> (the second quote) is the intended meaning.
            >>
            >> Does this clear things up?
            >>
            >> Damien
            >>[/color]
            >
            >[/color]


            Comment

            • Bruce Barker

              #7
              Re: Only static (Shared) types are thread safe ?!

              when the docs refer to members thread safety, they are refering to
              accessing that property from different threads.

              when they say statics are thread safe, but instance are not, they mean, that
              the static can be access from any thread safefully (as locking as been
              implementd), but instance members can only be safely accessed from the
              creating thread.

              if an non-thread safe instance member is to be accessed from multiple
              threads, you must add code to support this.

              -- bruce (sqlwork.com)


              "dee" <dee@home.net > wrote in message
              news:uHa54sirFH A.3340@TK2MSFTN GP15.phx.gbl...[color=blue]
              > OleDbCommand class like many .NET classes has the following description in
              > its help file:
              >
              > "Thread Safety
              > Any public static (Shared in Visual Basic) members of this type are safe
              > for multithreaded operations. Any instance members are not guaranteed to
              > be thread safe."
              > I have 2 questions:
              >
              > 1. I thought dynamic variables are thread-safe since threads have their
              > own stack and static data had to be synchronized. However, many many .NET
              > classes say ONLY static members of thier type are thread safe. Why is
              > that?
              >
              >
              > 2. Do we still have to synchronize access to this shared data?
              >
              > 3. If only static members are thread-safe why all the examples use non
              > static data?
              >
              > Thanks.
              > Dee
              >[/color]


              Comment

              • dee

                #8
                Re: Only static (Shared) types are thread safe ?!

                Thanks Bruce

                "Bruce Barker" <brubar_nospamp lease_@safeco.c om> wrote in message
                news:ut3jsFnrFH A.2076@TK2MSFTN GP14.phx.gbl...[color=blue]
                > when the docs refer to members thread safety, they are refering to
                > accessing that property from different threads.
                >
                > when they say statics are thread safe, but instance are not, they mean,
                > that the static can be access from any thread safefully (as locking as
                > been implementd), but instance members can only be safely accessed from
                > the creating thread.
                >
                > if an non-thread safe instance member is to be accessed from multiple
                > threads, you must add code to support this.
                >
                > -- bruce (sqlwork.com)
                >
                >
                > "dee" <dee@home.net > wrote in message
                > news:uHa54sirFH A.3340@TK2MSFTN GP15.phx.gbl...[color=green]
                >> OleDbCommand class like many .NET classes has the following description
                >> in its help file:
                >>
                >> "Thread Safety
                >> Any public static (Shared in Visual Basic) members of this type are safe
                >> for multithreaded operations. Any instance members are not guaranteed to
                >> be thread safe."
                >> I have 2 questions:
                >>
                >> 1. I thought dynamic variables are thread-safe since threads have their
                >> own stack and static data had to be synchronized. However, many many .NET
                >> classes say ONLY static members of thier type are thread safe. Why is
                >> that?
                >>
                >>
                >> 2. Do we still have to synchronize access to this shared data?
                >>
                >> 3. If only static members are thread-safe why all the examples use non
                >> static data?
                >>
                >> Thanks.
                >> Dee
                >>[/color]
                >
                >[/color]


                Comment

                • Damien

                  #9
                  Re: Only static (Shared) types are thread safe ?!

                  dee wrote:[color=blue]
                  > OleDbCommand class doesnt have any static members anyway
                  > what is the point of mentioning static members are thread safe ??
                  >[/color]
                  It's just a boilerplate statement that they've liberally sprinkled
                  throughout the documentation. I wouldn't try to read too much into it.

                  In the general case (when you're not passing instances around between
                  threads), it's only the static members that you would be concerned
                  about w.r.t thread safety, since those are the only ones which multiple
                  threads might conceivably call (on the same object, in this case the
                  type). Once you start passing instances between threads is when you
                  start having to think about implementing mutual exclusion (unless the
                  documentation, in that case, states that all members are thread safe).

                  HTH,

                  Damien

                  Comment

                  • Scott Allen

                    #10
                    Re: Only static (Shared) types are thread safe ?!

                    On Wed, 31 Aug 2005 11:10:15 -0400, "dee" <dee@home.net > wrote:
                    [color=blue]
                    >
                    >When they day "member" then mean either a method or a property?
                    >"instance member" could be a method that uses a static proptery? If so, then
                    >they should be handled like other static methods since thier user does
                    >doesnt know how they are implemented.
                    >
                    >Thanks for your time.
                    >Dee
                    >[/color]

                    Yes, members would include both properties and methods.

                    --
                    Scott



                    Comment

                    • dee

                      #11
                      Re: Only static (Shared) types are thread safe ?!

                      Thanks Damien

                      "Damien" <Damien_The_Unb eliever@hotmail .com> wrote in message
                      news:1125559838 .464836.3980@g4 9g2000cwa.googl egroups.com...[color=blue]
                      > dee wrote:[color=green]
                      >> OleDbCommand class doesnt have any static members anyway
                      >> what is the point of mentioning static members are thread safe ??
                      >>[/color]
                      > It's just a boilerplate statement that they've liberally sprinkled
                      > throughout the documentation. I wouldn't try to read too much into it.
                      >
                      > In the general case (when you're not passing instances around between
                      > threads), it's only the static members that you would be concerned
                      > about w.r.t thread safety, since those are the only ones which multiple
                      > threads might conceivably call (on the same object, in this case the
                      > type). Once you start passing instances between threads is when you
                      > start having to think about implementing mutual exclusion (unless the
                      > documentation, in that case, states that all members are thread safe).
                      >
                      > HTH,
                      >
                      > Damien
                      >[/color]


                      Comment

                      • dee

                        #12
                        Re: Only static (Shared) types are thread safe ?!

                        Thanks Allen.

                        "Scott Allen" <scott@nospam.o detocode.com> wrote in message
                        news:lq1eh1dlli 8gp945j25ho3l85 91utag3cf@4ax.c om...[color=blue]
                        > On Wed, 31 Aug 2005 11:10:15 -0400, "dee" <dee@home.net > wrote:
                        >[color=green]
                        >>
                        >>When they day "member" then mean either a method or a property?
                        >>"instance member" could be a method that uses a static proptery? If so,
                        >>then
                        >>they should be handled like other static methods since thier user does
                        >>doesnt know how they are implemented.
                        >>
                        >>Thanks for your time.
                        >>Dee
                        >>[/color]
                        >
                        > Yes, members would include both properties and methods.
                        >
                        > --
                        > Scott
                        > http://www.OdeToCode.com/blogs/scott/
                        >
                        >[/color]


                        Comment

                        Working...