boost mutex condition

来源:互联网 发布:ssh 远程端口转发 编辑:程序博客网 时间:2024/04/28 08:42

//============================================================================
// Name        : Test.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : in C++, Ansi-style
//============================================================================

 

#include <string>
#include <iostream>
#include <stdlib.h>
#include <windows.h>

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

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

boost::mutex io_mutex ;

class buffer
{
public :
    typedef boost::mutex::scoped_lock scoped_lock ;

    buffer():usTell(0) ,usHead(0) ,usSize(0)
    {

    }

    void put(int m)
    {
        scoped_lock lock(mutex) ;

        {
            scoped_lock lock(io_mutex) ;
            std::cout<< "put lock mutex start  ," <<std::endl ;
        }

        if(BUF_SIZE == usSize)
        {
            {
                scoped_lock lock(io_mutex) ;
                std::cout<<"Buffer is full .Waiting..." << std::endl;
            }

            while(BUF_SIZE == usSize)
            {
                std::cout<<"put wait cond.wait " <<std::endl ;
                cond.wait(lock);
            }
        }

        buf[usTell] = m ;
        usTell = (usTell+1)%BUF_SIZE ;
        ++usSize ;

        cond.notify_one() ;

        {
            scoped_lock lock(io_mutex) ;
            std::cout<< "put lock mutex end ," <<std::endl ;
        }
    }

    int get()
    {
        scoped_lock lk(mutex) ;

        {
            scoped_lock lock(io_mutex) ;
            std::cout<< "get lock mutex start ," <<std::endl ;
        }

        if(0 == usSize)
        {
            {
                scoped_lock lock(io_mutex) ;
                std::cout<<"Buffer is empty . Waiting ..." << std::endl;
            }

            while(0 == usSize)
            {
                std::cout<<"get full = 0 cond.wait " <<std::endl ;
                cond.wait(lk);
            }
        }

        int i = buf[usHead] ;
        usHead = (usHead+1)%BUF_SIZE ;
        --usSize ;
        cond.notify_one() ;

        {
            scoped_lock lock(io_mutex) ;
            std::cout<< "get lock mutex end ," <<std::endl ;
        }
        return i ;
    }


private:
    boost::mutex mutex ;
    boost::condition cond ;
    unsigned int usTell ;
    unsigned int usHead ;
    unsigned int usSize ;
    int buf[BUF_SIZE];
};

buffer buf ;

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

        buf.put(i);
    }
}

void reader()
{
    for(unsigned int i = 0 ; i < ITERS ; i++)
    {
        int n = buf.get() ;

        {
            boost::mutex::scoped_lock lock(io_mutex) ;
            std::cout<<"received:" << n << std::endl ;
        }
    }
}

int main()
{
    boost::thread thr1(&reader) ;
    boost::thread thr2(&writer) ;
    thr1.join() ;
    thr2.join() ;
    std::cout << "hehe \n" ;
    return 0;
}

 

get lock mutex start ,
Buffer is empty . Waiting ...
get full = 0 cond.wait
sending :0
put lock mutex start  ,
put lock mutex end ,

sending :1
get lock mutex end ,
received:0
put lock mutex start  ,
put lock mutex end ,
sending :2
get lock mutex start ,
get lock mutex end ,
received:1
put lock mutex start  ,
put lock mutex end ,
sending :3
get lock mutex start ,
get lock mutex end ,
received:2
put lock mutex start  ,
put lock mutex end ,
sending :4
get lock mutex start ,
get lock mutex end ,

这是运行的部分结果 , 有点不明白。 第二个锁是怎么进入到第一个锁的。

原创粉丝点击