473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sample Code: Blocking Queue


BlockingQueue implements a single-writer single-reader queue, something I find
myself needing frequently when working with multiple threads and internet
protocols.

After an instance of BlockingQueue is created the reader can invoke Dequeue and
will be blocked until either there is some object in the queue, or until
BlockingQueue.EOF is true. Setting EOF will eventually result in a null being
returned from Dequeue, but only after all items Enqueued have been retrieved

Source in the Free Things & Other Stuff folder at www.abderaware.com
--
What the caterpillar calls the end,
the rest of the world calls a butterfly.
~ Lao-tzu

Components For Thinkers
www.abderaware.com
zane @at@ abderaware .dot. com
Nov 15 '05 #1
2 5529
Zane Thomas [.NET/C# MVP] <za**@abderaware.com> wrote:
BlockingQueue implements a single-writer single-reader queue, something I find
myself needing frequently when working with multiple threads and internet
protocols.

After an instance of BlockingQueue is created the reader can invoke Dequeue and
will be blocked until either there is some object in the queue, or until
BlockingQueue.EOF is true. Setting EOF will eventually result in a null being
returned from Dequeue, but only after all items Enqueued have been retrieved


At the moment, it's not quite threadsafe - you don't synchronize at all
before setting m_Queue to null or checking whether or not it *is* null.
In addition, the queue could be closed *after* the check for
m_Queue==null but *before* calling m_Queue.Dequeue(), in which case you
get a NullReferenceException.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon,
At the moment, it's not quite threadsafe ...


That's true, and while not an excuse for code put out for general use, it's not
an issue the way I use it. I'll make it thread-safe tonight or thereabouts.
Thanks!
--
What the caterpillar calls the end,
the rest of the world calls a butterfly.
~ Lao-tzu

Components For Thinkers
www.abderaware.com
zane @at@ abderaware .dot. com
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
3
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers...
31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
7
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate...
16
by: William Stacey [MVP] | last post by:
Anyone care to comment on if this non-blocking queue implementation is sound? /// <summary> /// Summary description for NonBlockingQueue. /// Modeled after:...
0
by: yaron | last post by:
Hi, I need a code sample for doing this: - support for multiple agents that know each other and can talk each other over a non-blocking TCP socket. each agent can send and recieve data...
13
by: Hendrik van Rooyen | last post by:
Hi, I would like to do the following as one atomic operation: 1) Append an item to a list 2) Set a Boolean indicator It would be almost like getting and holding the GIL, to prevent a...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
2
by: kdeveloper | last post by:
Hello Pythonists, I am building a non blocking socket server for incomming UDP packets. The server needs to run at least three threads: 1. getting data and pushing to "some" storage (at the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.