Looking for FIFO Lock in .net 2.0? [modified]
-
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 becauselock
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 FIFOCollection
, a FIFOLock
will do) So far, I tried replacinglock
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
-
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 becauselock
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 FIFOCollection
, a FIFOLock
will do) So far, I tried replacinglock
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
-
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
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
-
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
-
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
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
-
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
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
-
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
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
-
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 becauselock
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 FIFOCollection
, a FIFOLock
will do) So far, I tried replacinglock
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
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