On Thu, Sep 10, 2009 at 2:32 PM, John Dlugosz
You could use a ring buffer that the various writers access. Each does an atomic increment on the W index to obtain a unique index to use, and then populates that element of the buffer's array. The reader uses the R pointer to consume items. The trick is, you have several writers all filling their items at the same time, and the R comes along and tries to use one. The reader needs to know that the writer is done filling it. So, the reader sets a flag to indicate "not-ready" after it is done using it and before changing R. The writer populates the fields and then clears the flag, indicating "ready", last. Now the reader knows to wait if it gets to an element where the flag is still set, even though W has moved ahead.
If that particular writing thread stalls (or crashes!?), the reader thread may have to wait "forever", even though other writes are finished and waiting. Not impossible to handle, but something to keep in mind. Tony
To preserve the atomicness of the increment-and-read, the W is always incrementing, and never resets. Instead, the writer has to do the modulo of the ring buffer size when it uses it.
The other hard part is watching for empty or full states. Decide how to tell them apart, and how to test for them asynchronously and without possibility of a deadlock.
The one I just did like that is specialized, and will never be full. I just sized the buffer to know how many entries are even possible in my application.
Let me know if you want to discuss it further.
--John
TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users