boost bind 引用计数生命周期
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;
}
#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;
}
#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;
}
#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;
}
#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函数增加引用计数,正确 用法。