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. Circular list as vector, lock needed?

Circular list as vector, lock needed?

Scheduled Pinned Locked Moved C#
questioncsharpgraphicshelp
7 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.
  • G Offline
    G Offline
    George Nistor
    wrote on last edited by
    #1

    Hi I use System.Io.Ports and the serial interface to receive data in a Terminal like application. From the .Net driver I have a DataReceivedHandler where I receive call with each small part of information sent on the serial. the problem: I have seen sometime some "garbage info", data from previous read. I suspect the call come realy fast and it happens sometime to get old data. intention: I want to change my current implementation in this DataReceivedHandler to write in a circular list. This list I intend to be a vector where I put struct having 2 fiels: - a State bit or boolean flag - and the information When I write I just check the state bit (which is similar to a lock), and if it is still false I write the info and increase the indexWrite. and now the question: Whould be safe this implementation without using lock{ circularList } when I receive very fast calls to the DataReceivedHandler? george

    OriginalGriffO 1 Reply Last reply
    0
    • G George Nistor

      Hi I use System.Io.Ports and the serial interface to receive data in a Terminal like application. From the .Net driver I have a DataReceivedHandler where I receive call with each small part of information sent on the serial. the problem: I have seen sometime some "garbage info", data from previous read. I suspect the call come realy fast and it happens sometime to get old data. intention: I want to change my current implementation in this DataReceivedHandler to write in a circular list. This list I intend to be a vector where I put struct having 2 fiels: - a State bit or boolean flag - and the information When I write I just check the state bit (which is similar to a lock), and if it is still false I write the info and increase the indexWrite. and now the question: Whould be safe this implementation without using lock{ circularList } when I receive very fast calls to the DataReceivedHandler? george

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Probably not - IIRC none of the .NET Collections are intrinsically thread safe, and they should all be locked. Either use the .NET Queue[^] with a lock or homebrew a safe version. If you are just using a single in-feed and a single out-feed, you don't need to use locking if you write it correctly. One way is to use an array, with two indexes - input and output. If insert only affects the input, and remove only affects the output, the only time they need to both be checked is for a full/empty test, which shouldn't need a lock - it would if you used more than one input source, or more than one remover.

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      D L G 3 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Probably not - IIRC none of the .NET Collections are intrinsically thread safe, and they should all be locked. Either use the .NET Queue[^] with a lock or homebrew a safe version. If you are just using a single in-feed and a single out-feed, you don't need to use locking if you write it correctly. One way is to use an array, with two indexes - input and output. If insert only affects the input, and remove only affects the output, the only time they need to both be checked is for a full/empty test, which shouldn't need a lock - it would if you used more than one input source, or more than one remover.

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        D Offline
        D Offline
        dybs
        wrote on last edited by
        #3

        OriginalGriff wrote:

        IIRC none of the .NET Collections are intrinsically thread safe

        Actually, .NET 4 introduced the System.Collections.Concurrent namespace[^], which includes a ConcurrentQueue. However, IIRC there's the potential for a delayed garbage collection - the queue keeps a reference to the object even after it's dequeued until it's been overwritten in the queue's buffer. This becomes more of an issue with a large-capacity queue. Our workaround was to use the Mono implementation of the ConcurrentQueue instead (it doesn't have this problem).

        The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

        OriginalGriffO 1 Reply Last reply
        0
        • D dybs

          OriginalGriff wrote:

          IIRC none of the .NET Collections are intrinsically thread safe

          Actually, .NET 4 introduced the System.Collections.Concurrent namespace[^], which includes a ConcurrentQueue. However, IIRC there's the potential for a delayed garbage collection - the queue keeps a reference to the object even after it's dequeued until it's been overwritten in the queue's buffer. This becomes more of an issue with a large-capacity queue. Our workaround was to use the Mono implementation of the ConcurrentQueue instead (it doesn't have this problem).

          The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Didn't know that - have a five!

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Probably not - IIRC none of the .NET Collections are intrinsically thread safe, and they should all be locked. Either use the .NET Queue[^] with a lock or homebrew a safe version. If you are just using a single in-feed and a single out-feed, you don't need to use locking if you write it correctly. One way is to use an array, with two indexes - input and output. If insert only affects the input, and remove only affects the output, the only time they need to both be checked is for a full/empty test, which shouldn't need a lock - it would if you used more than one input source, or more than one remover.

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            OriginalGriff wrote:

            thread safe

            New in .NET 4, http://msdn.microsoft.com/en-us/library/dd997305.aspx[^] [Edit] Never mind. OP already posted that.

            Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Probably not - IIRC none of the .NET Collections are intrinsically thread safe, and they should all be locked. Either use the .NET Queue[^] with a lock or homebrew a safe version. If you are just using a single in-feed and a single out-feed, you don't need to use locking if you write it correctly. One way is to use an array, with two indexes - input and output. If insert only affects the input, and remove only affects the output, the only time they need to both be checked is for a full/empty test, which shouldn't need a lock - it would if you used more than one input source, or more than one remover.

              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

              G Offline
              G Offline
              George Nistor
              wrote on last edited by
              #6

              yes this was my intention to have to indexes one for read, one for write, and because it is in a Handlerfunction it should be executed very fast so I will use a vector - as the circular list (or Buffer). I will try this solution first, after the other with the Thread safe collections. thx

              G 1 Reply Last reply
              0
              • G George Nistor

                yes this was my intention to have to indexes one for read, one for write, and because it is in a Handlerfunction it should be executed very fast so I will use a vector - as the circular list (or Buffer). I will try this solution first, after the other with the Thread safe collections. thx

                G Offline
                G Offline
                George Nistor
                wrote on last edited by
                #7

                probably I need some kind of lightweight lock, bacause the call to the Handler can come realy fast and mess up with my array index, which is already done in ConcurrentQueue Class. Even if I use a state bit on every element the index could be messed up by another call just before it is used. I have been wondering how it is implemented. Would be possible this class: ConcurrentQueue Class to e implemented using just Interlock class witout any SpinWait or while loops?

                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