Skip to content

Instantly share code, notes, and snippets.

@ochinchina
Created January 20, 2015 08:47
Show Gist options
  • Save ochinchina/1a7acd89d18517c9261f to your computer and use it in GitHub Desktop.
Save ochinchina/1a7acd89d18517c9261f to your computer and use it in GitHub Desktop.
C++ library boost pool & object_pool usage demo
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
#include <iostream>
class TestObject {
public:
TestObject( int i = 0, int j = 0)
:i_( i ),
j_( j )
{
}
int getI() const {
return i_;
}
int getJ() const {
return j_;
}
void print( std::ostream& out ) const {
out << "TestObject{i:" << i_ << ",j:" << j_ << "}";
}
private:
int i_;
int j_;
};
inline std::ostream& operator<<( std::ostream& out, const TestObject& obj ) {
obj.print( out );
return out;
}
void testPool() {
boost::pool<> pool( 10 );
void* p = pool.malloc();
std::cout << p << std::endl;
pool.free( p );
p = pool.malloc();
std::cout << p << std::endl;
}
void testObjectPool() {
boost::object_pool<TestObject> pool;
TestObject* p = pool.construct();
std::cout << *p << std::endl;
pool.destroy( p );
p = pool.construct( 10, 11 );
std::cout << *p << std::endl;
pool.destroy( p );
}
int main( int argc, char** argv ) {
testPool();
testObjectPool();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment