|
|
|
|
|
by deadcanard
1477 days ago
|
|
It's because shared_ptr uses an atomic count to synchronise between threads while the Rust version is assuming only one thread. There is no equivalent in the standard C++ lib. Though it's very easy to write one. If you use gcc's libstdc++, the internal shared_ptr is actually templated on the lock policy so you can do something like:
template<typename T> using nt_shared_ptr = std::__shared_ptr<T, __gnu_cxx::_S_single>; Then if you use nt_shared_ptr instead, the code will be much simpler. That said, your C++ code is incorrect as well. It gives the ownership to the shared ptr of a stack variable. During the destruction of the shared_ptr, delete will be called on a stack address leading to undefined behavior |
|
https://www.boost.org/doc/libs/1_65_0/libs/smart_ptr/doc/htm...