Re: Queue object with strange behaviour??
- From: RobKinney1 <RobKinney1@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 17 Oct 2006 09:15:03 -0700
Marc,
This totally works! Perfectly! We were also having data loss in our logs
and this has totally fixed it including maintaining the correct order of the
log entries.
I have been reading Jon's page on threading. I am going to read more later
on this afternoon when I have more time. That is very important stuff as you
mentioned. I may have to go back in some of my other code and fix some
things.
On behalf of the IT team here, we thank you for taking the time to help us!!
This fix is like gold to us. Let me know if there is ever something I can
do for you.
Thanks,
Rob K
"Marc Gravell" wrote:
first... DO NOT check the Count whle you don't own the lock; this WILL.
break... as the film goes "maybe not today, maybe not tomorrow, but
some day, soon, and for the rest of your life". That's the joy of
threading. Alternative suggested below.
// note: terminate should be a "volatile bool"
bool continue = true;
while(continue) {
T item;
lock(LoggingQueue) { // obtain exclusive access
if(LoggingQueue.Count==0) { // I wouldn't worry about -ve myself...
up to you
if(terminate) { // shut down requested
continue = false; // queue is empty, so exit loop
} else { // queue is empty; wait for work
Monitor.Wait(LoggingQueue); // relinquish lock, wait for a
kick, and reaquire
continue; // start loop again
}
//// etc as per my previous post, with logging near the bottom (outside
of the lock)
Note: I advise you to go a bit closer to my sample code; otherwise
(with your code below), if a queue has built up, it will need to log
all of it before more can be added. My way only processes one item per
lock, *and* it processes that item *outside* of the lock, which means
that adding new items is not bound by IO.
Also; the ==1 was for a reason; as it stands, you are doing unnecessary
Pulse()s; we know that we only Wait() if the queue is empty, i.e.
Count==0; this only happens if, after adding, the Count is 1. If the
Count is 7, we can say for sure that the logging thread is not
Wait()ing, but rather is simply patiently waiting on the lock that we
hold. No need for a Pulse().
Since the two threads are now isolated, I would also remove the
Sleep(), but again, that is up to you.
I can't really explain (in a short post) all of why this works. Threads
are tricky. You should really read Jon's pages end to end. Then read it
again ;-p
http://www.yoda.arachsys.com/csharp/threads/
Marc
- References:
- Re: Queue object with strange behaviour??
- From: Marc Gravell
- Re: Queue object with strange behaviour??
- From: RobKinney1
- Re: Queue object with strange behaviour??
- From: Marc Gravell
- Re: Queue object with strange behaviour??
- From: RobKinney1
- Re: Queue object with strange behaviour??
- From: Marc Gravell
- Re: Queue object with strange behaviour??
- Prev by Date: Re: How to get inherited class type/name inside static base class meth
- Next by Date: Re: Switch statement alternative?
- Previous by thread: Re: Queue object with strange behaviour??
- Next by thread: DataGridView add row
- Index(es):
Relevant Pages
|