随笔 - 21  文章 - 0  trackbacks - 0
<2014年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿

随笔分类

随笔档案

文章分类

搜索

  •  

最新评论

阅读排行榜

评论排行榜

test boost bind 引用计数生命周期

#include <iostream>
#include  <boost/bind.hpp>
#include <boost/function.hpp>

#include <muduo/net/EventLoop.h>

using namespace muduo::net;

typedef boost::function<void()>  procFunction;


EventLoop* g_Eventloop;


class Test1
{

public:

    Test1()
    {
        std::cout<<"Construct Test1"<<"\n";
    }

    ~Test1()
    {
        std::cout<<"destruct Test1"<<"\n";
    }


    void testfunc()
    {
        std::cout<<"test func"<<std::endl;
        g_Eventloop->quit();
    }


    void test_add_func()
    {

        std::cout<<"========test func========"<<"\n";

        g_Eventloop->queueInLoop(boost::bind(&Test1::test_add_func, this));
    }
};




int main()
{

    EventLoop loop;

    g_Eventloop = &loop;

    boost::shared_ptr<Test1> ptrtest1(new Test1);

    std::cout<<"use_count"<<ptrtest1.use_count()<<"\n";

    //        loop.runAfter(5, boost::bind(&Test1::testfunc, ptrtest1));


    loop.runInLoop(boost::bind(&Test1::test_add_func, ptrtest1));

    std::cout<<"use_count: "<<ptrtest1.use_count()<<"\n";

    loop.loop();

    std::cout<<"use_count: "<<ptrtest1.use_count()<<"\n";

    return 0;

}


boost example test 2

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <muduo/net/EventLoop.h>

#include <iostream>
using namespace muduo::net;

typedef boost::function<void()>  procFunction;


EventLoop* g_Eventloop;


class Test1
{

public:

    Test1()
    {
        std::cout<<"Construct Test1"<<"\n";
    }

    ~Test1()
    {
        std::cout<<"destruct Test1"<<"\n";
    }
    
    
    void testfunc()
    {
        std::cout<<"test func"<<std::endl;
        g_Eventloop->quit();
    }
};




int main()
{
    EventLoop loop;
    
    g_Eventloop = &loop;
    
    boost::shared_ptr<Test1> ptrtest1(new Test1);

    std::cout<<"use_count"<<ptrtest1.use_count()<<"\n";

    boost::weak_ptr<Test1> wptr(ptrtest1);

       
    loop.runAfter(5, boost::bind(&Test1::testfunc, wptr));


    
    std::cout<<"use_count"<<ptrtest1.use_count()<<"\n";

    loop.loop();
    
    return 0;
}

无法通过weakptr绑定类成员函数,编译不通过。


boost bind test3
#include <iostream>
#include  <boost/bind.hpp>
#include <boost/function.hpp>

#include <muduo/net/EventLoop.h>
#include <muduo/net/EventLoopThread.h>
#include <muduo/base/CurrentThread.h>
using namespace muduo::net;

typedef boost::function<void()>  procFunction;


EventLoop* g_Eventloop;


class Test1
{

public:

    Test1()
    {
        std::cout<<"Construct Test1"<<"\n";
    }

    ~Test1()
    {
        std::cout<<"destruct Test1"<<"\n";
    }
    
    
    void testfunc()
    {
        std::cout<<"test func"<<std::endl;
        g_Eventloop->quit();
    }


   void test_add_func()
   {

      std::cout<<"========test  add func========"<<"\n";
      
      g_Eventloop->queueInLoop(boost::bind(&Test1::testfunc, this));
   }
};




int main()
{


        EventLoopThread thr2;
        EventLoop* loop = thr2.startLoop();
         
        g_Eventloop = loop; 
        
        { 
            boost::shared_ptr<Test1> ptrtest1(new Test1);

            std::cout<<"##################use_count: "<<ptrtest1.use_count()<<"\n";
                
            loop->runInLoop(boost::bind(&Test1::test_add_func, ptrtest1));

            std::cout<<"###############use_count:   "<<ptrtest1.use_count()<<"\n";
        }
         muduo::CurrentThread::sleepUsec(500 * 1000);

         // std::cout<<"##################use_count: "<<ptrtest1.use_count()<<"\n";
    
         return 0;

}

存在问题,test_add_func执行完之后,bind引用计数降为0,会析构的,但是又bind this了一次。


bind test4
#include <iostream>
#include  <boost/bind.hpp>
#include <boost/function.hpp>
#include  <boost/enable_shared_from_this.hpp>


#include <muduo/net/EventLoop.h>
#include <muduo/net/EventLoopThread.h>
#include <muduo/base/CurrentThread.h>
using namespace muduo::net;

typedef boost::function<void()>  procFunction;


EventLoop* g_Eventloop;


class Test1: public boost::enable_shared_from_this<Test1>
{

public:

    Test1()
    {
        std::cout<<"Construct Test1"<<"\n";
    }

    ~Test1()
    {
        std::cout<<"destruct Test1"<<"\n";
    }


    void testfunc()
    {
        std::cout<<"test func"<<std::endl;
        g_Eventloop->quit();
    }


   void test_add_func()
   {

      std::cout<<"========test  add func========"<<"\n";

      g_Eventloop->queueInLoop(boost::bind(&Test1::testfunc, shared_from_this()));
   }
};


int main()
{


        EventLoopThread thr2;
        EventLoop* loop = thr2.startLoop();

        g_Eventloop = loop;

        {
            boost::shared_ptr<Test1> ptrtest1(new Test1);

            std::cout<<"##################use_count: "<<ptrtest1.use_count()<<"\n";

            loop->runInLoop(boost::bind(&Test1::test_add_func, ptrtest1));

            std::cout<<"###############use_count:   "<<ptrtest1.use_count()<<"\n";
        }
         muduo::CurrentThread::sleepUsec(500 * 1000);

         // std::cout<<"##################use_count: "<<ptrtest1.use_count()<<"\n";

         return 0;

}

改为从enable_share_from_this派生之后,可以通过this函数增加引用计数,正确 用法。
posted on 2014-07-04 17:06 pizzx 阅读(590) 评论(0)  编辑 收藏 引用 所属分类: c++/boost

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理