is a shared pointer to std::thread a bad practice?

  • A+
Category:Languages

Does it make sense to use std::shared_ptr < std::thread> ? The logic is simple: if thread is not needed, delete it, if new is required - realocate it. is there any way to compare this concept with pooled threads?

I do know the exact ammount of threads in my system(I develop Image processing algorithm, and I wanted to give each child of "algorithm" class an individual thread (maybe to make it private, then no shared_ptr is required), where this algorithm will be running, and idle this private thread if no image was provided. Is it a bad concept?


You probably miss the fact fact std::thread destructor does not terminate the thread. As already mentioned in the comments, if detach or join was not called before, std::thread destructor calls std::terminate. In other words, std::shared_ptr<std::thread> is pretty useless.

A std::thread is a rather low-level object. You may like to have a look at:

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: