代码示例
#include
#include
#include
#include
#include
struct Base
{
Base() { std::cout << " Base::Base()\n"; }
// 注意:此处非虚析构函数 OK
~Base() { std::cout << " Base::~Base()\n"; }
};
struct Derived: public Base
{
Derived() { std::cout << " Derived::Derived()\n"; }
~Derived() { std::cout << " Derived::~Derived()\n"; }
};
void thr(std::shared_ptr p)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::shared_ptr lp = p; // 线程安全,虽然自增共享的 use_count
{
static std::mutex io_mutex;
std::lock_guard lk(io_mutex);
std::cout << "local pointer in a thread:\n"
<< " lp.get() = " << lp.get()
<< ", lp.use_count() = " << lp.use_count() << '\n';
}
}
void copy(std::shared_ptr p)
{
std::cout << "copy function\n"
<< ", p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << "\n\n\n";
}
int main()
{
std::shared_ptr p0 = std::shared_ptr(new Derived);
std::cout << "Created a shared Derived (as a pointer to Base)\n"
<< " p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count() << "\n\n\n";
// "operator =" will increase ref count
std::shared_ptr p01 = p0;
std::cout << "test operator =\n"
<< "p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count()
<< ", p01.get() = " << p01.get()
<< ", p01.use_count() = " << p01.use_count() << "\n\n\n";
// "copy construct" will increase ref count
std::shared_ptr p02(p0);
std::cout << "test copy construct\n"
<< "p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count()
<< ", p02.get() = " << p02.get()
<< ", p02.use_count() = " << p02.use_count() << "\n\n\n";
copy(p0);
std::cout << "test copy construct 1\n"
<< ", p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count() << "\n\n\n";
// reset will decrease ref count
p02.reset();
std::cout << "test reset\n"
<< "p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count()
<< ", p02.get() = " << p02.get()
<< ", p02.use_count() = " << p02.use_count() << "\n\n\n";
// destruct will decrease ref count
{
std::shared_ptr p03(p0);
std::cout << "Created a shared Derived (as a pointer to Base)\n"
<< "p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count()
<< ", p03.get() = " << p03.get()
<< ", p03.use_count() = " << p03.use_count() << "\n";
}
std::cout << "Created a shared Derived (as a pointer to Base)\n"
<< ", p0.get() = " << p0.get()
<< ", p0.use_count() = " << p0.use_count() << "\n\n\n";
std::shared_ptr p = std::make_shared();
std::cout << "Created a shared Derived (as a pointer to Base)\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
std::thread t1(thr, p), t2(thr, p), t3(thr, p);
p.reset(); // 从 main 释放所有权
std::cout << "Shared ownership between 3 threads and released\n"
<< "ownership from main:\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
t1.join(); t2.join(); t3.join();
std::cout << "All threads completed, the last one deleted Derived\n";
}
运行结果
Base::Base()
Derived::Derived()
Created a shared Derived (as a pointer to Base)
p0.get() = 0x25a8c20, p0.use_count() = 1
test operator =
p0.get() = 0x25a8c20, p0.use_count() = 2, p01.get() = 0x25a8c20, p01.use_count() = 2
test copy construct
p0.get() = 0x25a8c20, p0.use_count() = 3, p02.get() = 0x25a8c20, p02.use_count() = 3
copy function
, p.get() = 0x25a8c20, p.use_count() = 4
Created a shared Derived (as a pointer to Base)
, p0.get() = 0x25a8c20, p0.use_count() = 3
test reset
p0.get() = 0x25a8c20, p0.use_count() = 2, p02.get() = 0, p02.use_count() = 0
Created a shared Derived (as a pointer to Base)
p0.get() = 0x25a8c20, p0.use_count() = 3, p03.get() = 0x25a8c20, p03.use_count() = 3
Created a shared Derived (as a pointer to Base)
, p0.get() = 0x25a8c20, p0.use_count() = 2
Base::Base()
Derived::Derived()
Created a shared Derived (as a pointer to Base)
p.get() = 0x25a8c70, p.use_count() = 1
Shared ownership between 3 threads and released
ownership from main:
p.get() = 0, p.use_count() = 0
local pointer in a thread:
lp.get() = 0x25a8c70, lp.use_count() = 4
local pointer in a thread:
lp.get() = 0x25a8c70, lp.use_count() = 3
local pointer in a thread:
lp.get() = 0x25a8c70, lp.use_count() = 2
Derived::~Derived()
Base::~Base()
All threads completed, the last one deleted Derived
Derived::~Derived()
Base::~Base()
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$ g++ -o shared_ptr shared_ptr.cc -std=c++11 -lpthread
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$ ./shared_ptr
Base::Base()
Derived::Derived()
Created a shared Derived (as a pointer to Base)
p0.get() = 0x67bc20, p0.use_count() = 1
test operator =
p0.get() = 0x67bc20, p0.use_count() = 2, p01.get() = 0x67bc20, p01.use_count() = 2
test copy construct
p0.get() = 0x67bc20, p0.use_count() = 3, p02.get() = 0x67bc20, p02.use_count() = 3
copy function
, p.get() = 0x67bc20, p.use_count() = 4
test copy construct 1
, p0.get() = 0x67bc20, p0.use_count() = 3
test reset
p0.get() = 0x67bc20, p0.use_count() = 2, p02.get() = 0, p02.use_count() = 0
Created a shared Derived (as a pointer to Base)
p0.get() = 0x67bc20, p0.use_count() = 3, p03.get() = 0x67bc20, p03.use_count() = 3
Created a shared Derived (as a pointer to Base)
, p0.get() = 0x67bc20, p0.use_count() = 2
Base::Base()
Derived::Derived()
Created a shared Derived (as a pointer to Base)
p.get() = 0x67bc70, p.use_count() = 1
Shared ownership between 3 threads and released
ownership from main:
p.get() = 0, p.use_count() = 0
local pointer in a thread:
lp.get() = 0x67bc70, lp.use_count() = 4
local pointer in a thread:
lp.get() = 0x67bc70, lp.use_count() = 3
local pointer in a thread:
lp.get() = 0x67bc70, lp.use_count() = 2
Derived::~Derived()
Base::~Base()
All threads completed, the last one deleted Derived
Derived::~Derived()
Base::~Base()