Making a Resource Thread-Safe - C++ boost

C++ examples for boost:thread

Description

Making a Resource Thread-Safe

#include <iostream>
#include <boost/thread/thread.hpp>
#include <string>

template<typename T>
class Queue {
public:
   Queue() {}
  ~Queue() {}

   void enqueue(const T& x) {
      boost::mutex::scoped_lock lock(mutex_);
      list_.push_back(x);
   }

   T dequeue() {
      boost::mutex::scoped_lock lock(mutex_);

      if (list_.empty())
         throw "empty!";     // This leaves the current scope, so the
      T tmp = list_.front(); // lock is released
      list_.pop_front();
      return(tmp);
   }

private:
   std::list<T> list_;
   boost::mutex mutex_;
};

Queue<std::string> queueOfStrings;

void sendSomething() {
   std::string s;
   for (int i = 0; i < 10; ++i) {
      queueOfStrings.enqueue("Cyrus");
   }
}

void recvSomething() {
   std::string s;

   for (int i = 0; i < 10; ++i) {
      try {s = queueOfStrings.dequeue();}
      catch(...) {}
   }
}
int main() {
   boost::thread thr1(sendSomething);
   boost::thread thr2(recvSomething);

   thr1.join();
   thr2.join();
}

Related Tutorials