In this blog post (2010), someone is trying to solve the producer/consumer problem using Boost::strand facility. I have the feeling that he missed the point and that his program never runs simultaneously some producer and some consumer, but I'm not that expert of boost library to get confident about it.
- He's got only one strand, on which both
producer()
andconsumer()
calls are dispatched by some timers; - He's got two thread, both invoking
io_service::run()
Yet, one strand only with the guarantee that "none of those handlers will execute concurrently" also means that we'll be either producing or at a time, while I'd say nothing should prevent a producer from producing unit U+t while consumer uses unit U, right ?
void producer_consumer::producer() {
if ( count_ < num) {
++count_;
intvec_.push_back(count_);
std::cout << count_ < " pushed back into integer vector." << std::endl;
timer1_.async_wait(strand_.wrap(
boost::bind(&producer_consumer::producer, this))); // loops back
timer2_.async_wait(strand_.wrap(
boost::bind(&producer_consumer::consumer, this))); // start consumer
}
}
Or am I missing the fact that there would be some File::async_read()
accepting a strand-wrapped-"produce" function as completion callback and a similar Socket::ready-to-write-again that would explain that his proposal make sense as long as "producer()" and "consumer()" are actually the monitor-protected parts that interface with the shared buffer ?