Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/thread/mutex.hpp>
- #include <boost/thread.hpp>
- struct Consumer {
- void consume(const boost::shared_ptr<int> aBar)
- {
- const boost::unique_lock<boost::mutex> myLock(theMutex);
- thePtr = aBar;
- }
- void operator()() {
- while(1) {
- boost::shared_ptr<int> myLocal;
- {
- const boost::unique_lock<boost::mutex> myLock(theMutex);
- myLocal = thePtr;
- }
- myLocal.use_count();
- }
- }
- boost::shared_ptr<int> thePtr;
- boost::mutex theMutex;
- };
- struct Producer {
- Producer(Consumer& aConsumer) : theConsumer(aConsumer) {}
- void operator()() {
- while(1) {
- boost::shared_ptr<int> myBar(new int(3));
- theConsumer.consume(myBar);
- myBar.use_count();
- }
- }
- Consumer& theConsumer;
- };
- int main() {
- Consumer myConsumer;
- Producer myProducer(myConsumer);
- boost::thread myConsumerThread(boost::ref(myConsumer));
- boost::thread myProducerThread(boost::ref(myProducer));
- myProducerThread.join();
- myConsumerThread.join();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement