mercredi 26 avril 2017

Why create std::thread instances with shared_ptr instead of unique_ptr?

I've been using Jeff Langr's book Modern C++ Programming with Test Driven Development as base of a Threadpool program that i'm working on, adding features .

In his program he creates the threads using std::shared_ptr instead of std::unique_ptr. Is there any reason for using shared_ptrs ?

class ThreadPool {
  public:
  void start(unsigned int numberOfThreads) {
      for(unsigned int i{0}; i < numberOfThreads; i++) {
         threads.push_back(std::make_shared<std::thread>(&ThreadPool::worker, this));
      } 
  }

  std::vector<std::shared_ptr<std::thread>> threads;

Aucun commentaire:

Enregistrer un commentaire