c++ - shared_ptr reference counting when spawning a std::thread -


i'm little new multithreading in c++11 , have specific question on spawning std::threads shown below. notes class public method start in it. , thread_list vector holding std::threads.

void spawn() {     std::shared_ptr<notes> note = std::make_shared<notes>();     std::thread thrd(&notes::start, note);     thread_list.push_back(std::move(thrd)); } 

my question owns shared_ptr note after function finishes? reference thread thrd count reference , hence shared_ptr not destruct object reference? clarification appreciated.

my question owns shared_ptr note after function finishes?

thrd owns it, in way. not really. read on happens.

does reference thread thrd count reference , hence shared_ptr not destruct object reference?

kind of, not really. thrd doesn't count reference, , doesn't share ownership of note. it's handle thread.

so owns note? the notes::start function does.

you've started thread executing notes::start, , have passed notes function. function owns notes.

if thrd goes out of scope, notes still exist long notes::start function hasn't exited , hasn't relinquished ownership of notes.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -