Static variables thead safe?

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

    #1

    Static variables thead safe?

    Hi,

    Are static variables thread safe? I have a static byte array. This will be
    accessed from more than one thread. Is it automatically thread safe by being
    static rather than an instance or do i need to write code to make it thread
    safe?

    Cheers
    Macca
  • Peter Rilling

    #2
    Re: Static variables thead safe?

    No variable is automatically thread-safe, not even static.

    "Macca" <Macca@discussi ons.microsoft.c om> wrote in message
    news:65C70FA0-C15B-42D8-9BBA-E663FC98BCD3@mi crosoft.com...[color=blue]
    > Hi,
    >
    > Are static variables thread safe? I have a static byte array. This will be
    > accessed from more than one thread. Is it automatically thread safe by
    > being
    > static rather than an instance or do i need to write code to make it
    > thread
    > safe?
    >
    > Cheers
    > Macca[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Static variables thead safe?


      "Macca" <Macca@discussi ons.microsoft.c om> wrote in message
      news:65C70FA0-C15B-42D8-9BBA-E663FC98BCD3@mi crosoft.com...
      | Hi,
      |
      | Are static variables thread safe? I have a static byte array. This will be
      | accessed from more than one thread. Is it automatically thread safe by
      being
      | static rather than an instance or do i need to write code to make it
      thread
      | safe?
      |
      | Cheers
      | Macca

      No, certainly not a static variable, only local variables are by definition
      thread safe (for as long as they stay 'local').

      Willy.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Static variables thead safe?

        Macca <Macca@discussi ons.microsoft.c om> wrote:[color=blue]
        > Are static variables thread safe? I have a static byte array. This will be
        > accessed from more than one thread. Is it automatically thread safe by being
        > static rather than an instance or do i need to write code to make it thread
        > safe?[/color]

        I mentioned this a couple of days ago, but...

        If you have thread local static variables (i.e. static variables with
        the ThreadLocalAttr ibute applied), they're thread-safe by definition.
        Of course, if they have values which are references to objects which
        are used in multiple threads, then manipulating those objects needs to
        be made thread-safe, but the variables themselves are thread-safe.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Stoitcho Goutsev \(100\)

          #5
          Re: Static variables thead safe?

          [color=blue]
          > If you have thread local static variables (i.e. static variables with
          > the ThreadLocalAttr ibute applied), they're thread-safe by definition.
          > Of course, if they have values which are references to objects which
          > are used in multiple threads, then manipulating those objects needs to
          > be made thread-safe, but the variables themselves are thread-safe.[/color]

          I don't know what ThreadLocalAttr ibute is. There is no information on such
          an attribute in the docs (or at least I can't find it).
          If this is what I think it is - to create thread bound data using thread's
          data slots, I wouldn't say that static variables marked with
          ThreadLocalAttr ibute are thread safe. As I understand thread safety is that
          more than one thread can manipulate the variable without collision. If the
          data is thread bound that means it exist only for sepcific thread. In this
          case no other thread can use that data. This is no thread safety because
          only one thread can use the data.

          My answer to the question is - No, static variables by themselves are not
          thread safe.


          --
          HTH
          Stoitcho Goutsev (100)


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Static variables thead safe?

            Stoitcho Goutsev (100) <100@100.com> wrote:[color=blue][color=green]
            > > If you have thread local static variables (i.e. static variables with
            > > the ThreadLocalAttr ibute applied), they're thread-safe by definition.
            > > Of course, if they have values which are references to objects which
            > > are used in multiple threads, then manipulating those objects needs to
            > > be made thread-safe, but the variables themselves are thread-safe.[/color]
            >
            > I don't know what ThreadLocalAttr ibute is. There is no information on such
            > an attribute in the docs (or at least I can't find it).[/color]

            Oops, sorry - ThreadStaticAtt ribute.
            [color=blue]
            > If this is what I think it is - to create thread bound data using thread's
            > data slots, I wouldn't say that static variables marked with
            > ThreadLocalAttr ibute are thread safe. As I understand thread safety is that
            > more than one thread can manipulate the variable without collision. If the
            > data is thread bound that means it exist only for sepcific thread. In this
            > case no other thread can use that data. This is no thread safety because
            > only one thread can use the data.[/color]

            Well, that would rule Willy's answer incorrect too, of course ;)

            In the case of a ThreadStatic variable, it's a variable which can be
            used as if it were a normal variable, but happens to have a different
            value for each thread. This can be really, really handy in places...

            Whether you deem them to be "thread safe" or not really depends on how
            you define "thread safe" though, and that doesn't really have any
            universal definition that I'm aware of.
            [color=blue]
            > My answer to the question is - No, static variables by themselves are not
            > thread safe.[/color]

            Certainly not "vanilla" static variables.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Nick Hounsome

              #7
              Re: Static variables thead safe?


              "Macca" <Macca@discussi ons.microsoft.c om> wrote in message
              news:65C70FA0-C15B-42D8-9BBA-E663FC98BCD3@mi crosoft.com...[color=blue]
              > Hi,
              >
              > Are static variables thread safe? I have a static byte array. This will be
              > accessed from more than one thread. Is it automatically thread safe by
              > being
              > static rather than an instance or do i need to write code to make it
              > thread
              > safe?[/color]

              It depends on what you mean by thread-safe.
              It wont crash.


              Comment

              Working...