vendredi 27 juillet 2018

Vector of elements containing std::threads

I've got a class "Tester" containing a std:thread object, and an std::vector of "Tester". I understand that I can't copy threads, so the push_back is out of question, but why emplace_back is not working? Where is the copy in my code?

#include <iostream>
#include <thread>
#include <vector>
#include <functional>

#include <unistd.h>

class Tester
{
  public:
  Tester(std::function<void(void)> func) : 
    th(func)
  {
  }

  ~Tester()
  {
    th.join()
  }

  private:
  std::thread th;
};

std::vector<Tester> testers;

void InnerHelloWorld()
{
  std::cout << "Hello from the inner word!\n";
}

int main() {
  std::cout << "Hello World!\n";

  for(size_t i = 0 ; i < 4 ; i++)
  {
    testers.emplace_back(InnerHelloWorld);
  }

  sleep(1);

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire