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. Looking for FIFO Lock in .net 2.0? [modified]

Looking for FIFO Lock in .net 2.0? [modified]

Scheduled Pinned Locked Moved C#
helpannouncementcsharpcomsysadmin
8 Posts 3 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.
  • F Offline
    F Offline
    Firo Atrum Ventus
    wrote on last edited by
    #1

    Hi guys, I'm currently working on a ship management system. The problem lies in the main bridge between the ships and the server. When there is more than 700 ships, the bridge doesn't send the message to the server in FIFO order, causing the display in the application to appear as jumping back and forth. After a bit of debugging (and searching of course), I found that the root of the problem is within lock(){} which process data within a list just before sending it to server. There are 10 Threads managing this List, All of them does the same job (The previous version which use just 1 list doesn't have this error but it's very slow and it has a huge memory usage). This is because lock release thread according priority, causing the latest ship data to be sent before the older ship data. I do realize that .net doesn't have any FIFO Lock and I do realize the existence of ConcurrentQueue[^] and other thread safe collection, but this application must run on .net 2.0 (dunno why). So is there any solution to this problem? (It's not necessarily a FIFO Collection, a FIFO Lock will do) So far, I tried replacing lock with this class:

    using System.Threading;
    
    public sealed class QueuedLock
    {
        private object innerLock;
        private volatile int ticketsCount = 0;
        private volatile int ticketToRide = 1;
    
        public QueuedLock()
        {
            innerLock = new Object();
        }
    
        public void Enter()
        {
            int myTicket = Interlocked.Increment(ref ticketsCount);
            Monitor.Enter(innerLock);
            while (true)
            {
    
                if (myTicket == ticketToRide)
                {
                    return;
                }
                else
                {
                    Monitor.Wait(innerLock);
                }
            }
        }
    
        public void Exit()
        {
            Interlocked.Increment(ref ticketToRide);
            Monitor.PulseAll(innerLock);
            Monitor.Exit(innerLock);
        }
    }
    

    But it doesn't help. In fact, it increases the error percentage from 20% (Simulated with a generator that generate 100.000 ship data) to 23% :wtf: [Edit:Miscalculation on error count :-O ] So, does a

    D L 2 Replies Last reply
    0
    • F Firo Atrum Ventus

      Hi guys, I'm currently working on a ship management system. The problem lies in the main bridge between the ships and the server. When there is more than 700 ships, the bridge doesn't send the message to the server in FIFO order, causing the display in the application to appear as jumping back and forth. After a bit of debugging (and searching of course), I found that the root of the problem is within lock(){} which process data within a list just before sending it to server. There are 10 Threads managing this List, All of them does the same job (The previous version which use just 1 list doesn't have this error but it's very slow and it has a huge memory usage). This is because lock release thread according priority, causing the latest ship data to be sent before the older ship data. I do realize that .net doesn't have any FIFO Lock and I do realize the existence of ConcurrentQueue[^] and other thread safe collection, but this application must run on .net 2.0 (dunno why). So is there any solution to this problem? (It's not necessarily a FIFO Collection, a FIFO Lock will do) So far, I tried replacing lock with this class:

      using System.Threading;
      
      public sealed class QueuedLock
      {
          private object innerLock;
          private volatile int ticketsCount = 0;
          private volatile int ticketToRide = 1;
      
          public QueuedLock()
          {
              innerLock = new Object();
          }
      
          public void Enter()
          {
              int myTicket = Interlocked.Increment(ref ticketsCount);
              Monitor.Enter(innerLock);
              while (true)
              {
      
                  if (myTicket == ticketToRide)
                  {
                      return;
                  }
                  else
                  {
                      Monitor.Wait(innerLock);
                  }
              }
          }
      
          public void Exit()
          {
              Interlocked.Increment(ref ticketToRide);
              Monitor.PulseAll(innerLock);
              Monitor.Exit(innerLock);
          }
      }
      

      But it doesn't help. In fact, it increases the error percentage from 20% (Simulated with a generator that generate 100.000 ship data) to 23% :wtf: [Edit:Miscalculation on error count :-O ] So, does a

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      Yes, System.Collections.Queue is a FIFO class. http://msdn.microsoft.com/en-us/library/system.collections.queue(v=VS.80).aspx[^]

      Dave Find Me On: Web|Facebook|Twitter|LinkedIn


      Folding Stats: Team CodeProject

      F 1 Reply Last reply
      0
      • D DaveAuld

        Yes, System.Collections.Queue is a FIFO class. http://msdn.microsoft.com/en-us/library/system.collections.queue(v=VS.80).aspx[^]

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


        Folding Stats: Team CodeProject

        F Offline
        F Offline
        Firo Atrum Ventus
        wrote on last edited by
        #3

        Um,, I think I use wrong title for my thread. Fixed now. Nevertheless, I'll try using Queue as described here[^]. [Edit] Error percentage:6% [Edit2:Miscalculation :-O ] Well, at least it's better than my solution. :laugh: Yosh, I'm making progress, thanks :-D [/edit]

        Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

        D 1 Reply Last reply
        0
        • F Firo Atrum Ventus

          Um,, I think I use wrong title for my thread. Fixed now. Nevertheless, I'll try using Queue as described here[^]. [Edit] Error percentage:6% [Edit2:Miscalculation :-O ] Well, at least it's better than my solution. :laugh: Yosh, I'm making progress, thanks :-D [/edit]

          Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #4

          It does show you how to lock the queue in the middle of the page.

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          F 1 Reply Last reply
          0
          • D DaveAuld

            It does show you how to lock the queue in the middle of the page.

            Dave Find Me On: Web|Facebook|Twitter|LinkedIn


            Folding Stats: Team CodeProject

            F Offline
            F Offline
            Firo Atrum Ventus
            wrote on last edited by
            #5

            Well, yeah. But what I'm looking for is to make a FIFO Lock in .net 2.0. Anyway, thanks, guess I'll experiment a bit more :)

            Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

            D 1 Reply Last reply
            0
            • F Firo Atrum Ventus

              Well, yeah. But what I'm looking for is to make a FIFO Lock in .net 2.0. Anyway, thanks, guess I'll experiment a bit more :)

              Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #6

              Now you have confused me! You are trying to re-invent the wheel? QUEUE is FIFO Look at the section in the link I sent you, it discusses thread safety and locks via a Synchronize wrapper.... Eveything is there for you. Good luck :)

              Dave Find Me On: Web|Facebook|Twitter|LinkedIn


              Folding Stats: Team CodeProject

              F 1 Reply Last reply
              0
              • D DaveAuld

                Now you have confused me! You are trying to re-invent the wheel? QUEUE is FIFO Look at the section in the link I sent you, it discusses thread safety and locks via a Synchronize wrapper.... Eveything is there for you. Good luck :)

                Dave Find Me On: Web|Facebook|Twitter|LinkedIn


                Folding Stats: Team CodeProject

                F Offline
                F Offline
                Firo Atrum Ventus
                wrote on last edited by
                #7

                DaveAuld wrote:

                You are trying to re-invent the wheel?

                Yes, I got that class from this page[^]. It says that there is no FIFO locks in .net and I have to create my own.

                Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

                1 Reply Last reply
                0
                • F Firo Atrum Ventus

                  Hi guys, I'm currently working on a ship management system. The problem lies in the main bridge between the ships and the server. When there is more than 700 ships, the bridge doesn't send the message to the server in FIFO order, causing the display in the application to appear as jumping back and forth. After a bit of debugging (and searching of course), I found that the root of the problem is within lock(){} which process data within a list just before sending it to server. There are 10 Threads managing this List, All of them does the same job (The previous version which use just 1 list doesn't have this error but it's very slow and it has a huge memory usage). This is because lock release thread according priority, causing the latest ship data to be sent before the older ship data. I do realize that .net doesn't have any FIFO Lock and I do realize the existence of ConcurrentQueue[^] and other thread safe collection, but this application must run on .net 2.0 (dunno why). So is there any solution to this problem? (It's not necessarily a FIFO Collection, a FIFO Lock will do) So far, I tried replacing lock with this class:

                  using System.Threading;
                  
                  public sealed class QueuedLock
                  {
                      private object innerLock;
                      private volatile int ticketsCount = 0;
                      private volatile int ticketToRide = 1;
                  
                      public QueuedLock()
                      {
                          innerLock = new Object();
                      }
                  
                      public void Enter()
                      {
                          int myTicket = Interlocked.Increment(ref ticketsCount);
                          Monitor.Enter(innerLock);
                          while (true)
                          {
                  
                              if (myTicket == ticketToRide)
                              {
                                  return;
                              }
                              else
                              {
                                  Monitor.Wait(innerLock);
                              }
                          }
                      }
                  
                      public void Exit()
                      {
                          Interlocked.Increment(ref ticketToRide);
                          Monitor.PulseAll(innerLock);
                          Monitor.Exit(innerLock);
                      }
                  }
                  

                  But it doesn't help. In fact, it increases the error percentage from 20% (Simulated with a generator that generate 100.000 ship data) to 23% :wtf: [Edit:Miscalculation on error count :-O ] So, does a

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Dave is right, a Queue is what you seem to need here. And don't think of it as a QueuedLock, look for a LockedQueue, i.e. a queue that uses a single lock to make it thread-safe. That is bound to keep your data in FIFO order. You can easily build a LockedQueue yourself, or just google the term to find several examples. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  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