|
Boost Users : |
Subject: [Boost-users] [thread] Double Buffer Example
From: Christian Henning (chhenning_at_[hidden])
Date: 2011-02-18 18:12:49
Hi there, I have some example code for boost::thread that illustrates
a common multi-threaded design pattern. It's called Double Buffering
which, I believe, is commonly used when a consumer is likely to be
much slower processing items than a producer producing them. In this
case a message queue is not really needed since a two element queue is
as good as n element queue. Just imagine the case when a
saving_to_database thread operates at 10MBit per second while a
producer thread generates database rows at a 15MBit per second.
For this example I decided to create a class binary_semaphore which
simulates an event object that can either be set or unset. BTW,
wouldn't a semaphore class be a nice addition to boost::thread? The
code can be found here:
http://pastebin.com/ASJp0t5h
The code for the example is here:
http://pastebin.com/VchGg8ci
I think it would be great idea to add some simple pattern examples in
the boost/libs/thread/example folder.
Please let me know if there is a bug or potential race condition in the example.
Regards,
Christian
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net