C++
Email
Discuss
Print
Reprint

add to:
Del.icio.us
Digg
Google
Spurl
Slashdot
Y! MyWeb
Blink
Furl
April 15, 2003

The Boost.Threads Library

(Page 5 of 7)
May 2002/The Boost.Threads Library/Listing 4

Listing 4: The boost::condition class

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <iostream>

const int BUF_SIZE = 10;
const int ITERS = 100;

boost::mutex io_mutex;

class buffer
{
public:
  typedef boost::mutex::scoped_lock
    scoped_lock;
    
  buffer()
    : p(0), c(0), full(0)
  {
  }
    
  void put(int m)
  {
    scoped_lock lock(mutex);
    if (full == BUF_SIZE)
    {
      {
        boost::mutex::scoped_lock
          lock(io_mutex);
        std::cout <<
          "Buffer is full. Waiting..."
          << std::endl;
      }
      while (full == BUF_SIZE)
        cond.wait(lock);
    }
    buf[p] = m;
    p = (p+1) % BUF_SIZE;
    ++full;
    cond.notify_one();
  }

  int get()
  {
    scoped_lock lk(mutex);
    if (full == 0)
    {
      {
        boost::mutex::scoped_lock
          lock(io_mutex);
        std::cout <<
          "Buffer is empty. Waiting..."
          << std::endl;
      }
      while (full == 0)
        cond.wait(lk);
    }
    int i = buf[c];
    c = (c+1) % BUF_SIZE;
    --full;
    cond.notify_one();
    return i;
  }
    
private:
  boost::mutex mutex;
  boost::condition cond;
  unsigned int p, c, full;
  int buf[BUF_SIZE];
};

buffer buf;

void writer()
{
  for (int n = 0; n < ITERS; ++n)
  {
    {
      boost::mutex::scoped_lock
        lock(io_mutex);
      std::cout << "sending: "
        << n << std::endl;
    }
    buf.put(n);
  }
}

void reader()
{
  for (int x = 0; x < ITERS; ++x)
  {
    int n = buf.get();
    {
      boost::mutex::scoped_lock
        lock(io_mutex);
      std::cout << "received: "
        << n << std::endl;
    }
  }
}
    
int main(int argc, char* argv[])
{
  boost::thread thrd1(&reader);
  boost::thread thrd2(&writer);
  thrd1.join();
  thrd2.join();
  return 0;
}
— End of Listing —
Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 Next Page


Please log in to post comments.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    INFO-LINK
     


    MARKETPLACE
    Ensure global WAN & LAN network availability
    Find out how to ensure global WAN and LAN network availability for all networked financial and banking applications across distributed organizations. Free white papers from NetScout.
    Find Solutions from IBMıs Business Partners
    Explore solutions that have proven success helping clients meet marketplace demands and align with IBM hardware and middleware platforms.
    Keep your Network Running!
    Build an Environment with Enhanced Security & Resilience with IBM.
    Making Applications Mean Business!
    Do your applications create barriers to desired business goals? IBM can help! Click to download complimentary reports from IBM and learn how.
    IT Sales Lead Follow Up
    Fast and acurate IT lead qualification from websites, trade shows, direct mail, trade advertising or any other marketing sources. Don't let good leads die young. SSM: Strategic Sales & Marketing. Technology Lead Generation: Since 1989

    |
    SDMG Websites: BYTE.com, DotNetJunkies, MSDN Magazine, Sys Admin, SD Expo, SqlJunkies, TechNet Magazine, Unixreview