Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Arrays and Threads

Arrays and Threads

Scheduled Pinned Locked Moved C#
csharpdatabasedata-structuresperformancequestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    albean
    wrote on last edited by
    #1

    In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?

    L J D 4 Replies Last reply
    0
    • A albean

      In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      AFAI understand it, you need to create a syncronized version of the collection/set , that would allow many threads to read, but only one to write. I dont think u need to lock the entire array though, isnt it just possible to lock the element? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      1 Reply Last reply
      0
      • A albean

        In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?

        J Offline
        J Offline
        J Dunlap
        wrote on last edited by
        #3

        You're probably best off working with a collection, as .NET collections have built-in synchronization features. Look it up on MSDN.

        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
        "You must be the change you wish to see in the world." - Mahatma Gandhi

        1 Reply Last reply
        0
        • A albean

          In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Have a look at ArrayList.Syncronized(IList) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          1 Reply Last reply
          0
          • A albean

            In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #5

            albean wrote: I ask because an array in C# is more than just a chunk of memory. Not exactly. One-dimension arrays in C# are actually objects that store a pointer to a C-style array, so what you are asking is actually safe on C#, if your algorithm is really thread safe. ORACLE One Real A$#h%le Called Lary Ellison

            A 1 Reply Last reply
            0
            • D Daniel Turini

              albean wrote: I ask because an array in C# is more than just a chunk of memory. Not exactly. One-dimension arrays in C# are actually objects that store a pointer to a C-style array, so what you are asking is actually safe on C#, if your algorithm is really thread safe. ORACLE One Real A$#h%le Called Lary Ellison

              A Offline
              A Offline
              albean
              wrote on last edited by
              #6

              Excellent explanation! Thanks Daniel.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups