Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2005

Tip: Looking for answers? Try searching our database.

Threading with a Queue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cool Guy - 24 May 2005 18:05 GMT
The main thread of my application will add items to a queue, which another
thread will dequeue items from.  This other thread will then perform a
blocking operation with each item it finds.

Is it possible to do this in a thread-safe manner?

I imagine something like this would work:

// ran in background thread
void DequeuerThreadMethod()
{
 while (!Finished)
 {
   object item;
   lock (queue)
   {
     item = queue.Dequeue();
   }
   DoBlockingOperation(item);

   lock (queue)
   {
     if (queue.Count == 0)
       SuspendThisThread();
   }
 }
}

// ran in main application thread
void AddToQueue(object item)
{
 lock (queue)
 {
   queue.Enqueue(item);
 }
 ResumeDequeuerThread();
}

Will this be thread-safe?
Jon Skeet [C# MVP] - 24 May 2005 18:39 GMT
> The main thread of my application will add items to a queue, which another
> thread will dequeue items from.  This other thread will then perform a
> blocking operation with each item it finds.
>
> Is it possible to do this in a thread-safe manner?

Absolutely - but it's better to use Monitor.Wait/Pulse than suspending
and resuming the thread directly.

See half way down
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
for some sample code.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Cool Guy - 24 May 2005 19:40 GMT
> See half way down
> http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
> for some sample code.

Ah.

I have a question on that.  Why is there a while clause in
ProducerConsumer.Consume, instead of an if clause?  Is it possible for
queue.Count to be 0 when Monitor.Wait exits, considering the implementation
of that class?
Jon Skeet [C# MVP] - 24 May 2005 20:23 GMT
> > See half way down
> > http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
[quoted text clipped - 4 lines]
> queue.Count to be 0 when Monitor.Wait exits, considering the implementation
> of that class?

Yes - consider the case where there are two threads consuming work
items, and one producing.

Consumer 1                 Consumer 2            Producer

Enter listLock
queue.Count==0 - yes
Wait for listLock

                                                Enter listLock

                         Enter listLock (start)

                                                Queue item
                                                queue.Count==1 - yes
                                                Pulse listLock
                                                Exit listLock

                         Enter listLock (complete)
                         queue.Count==0 - no
                         Dequeue item
                         Exit listLock

Wake up
Reaquire listLock
queue.Count is still 0, so need to wait again

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Cool Guy - 24 May 2005 22:58 GMT
> Consumer 1                 Consumer 2            Producer
>
[quoted text clipped - 19 lines]
> Reaquire listLock
> queue.Count is still 0, so need to wait again

Ah, of course!  My confused arose by me thinking that the call to
Monitor.Pulse would make the call to Monitor.Wait return immediately --
before anything else aquires a lock.

Jon, when I look up 'helpful' in the dictionary, there is a picture of you.
;-)  Thanks!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.