Adrian
Adrian

Reputation: 20058

How can I add boost threads to a vector

I have something like this that is incorect:

vector<boost::thread> vec;
for(int agent = 1; agent <= numAgents; ++agent)
{
    boost::thread agentThread(sellTickets, agent, numTickets/numAgents);
    vec.push_back(agentThread);
}

Maybe i should add pointers to boost::thread in the vector, but then I don't know how to add dynamic allocated threads, how should I do to make this work ?

Thank you.

Upvotes: 8

Views: 4599

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72469

  • You must have a compiler with move-semantics supported in order to make your code work,
  • or use vector<shared_ptr<boost::thread>> with code like:

    vec.push_back(make_shared<boost::thread>(sellTickets, agent, numTickets/numAgents));
    
  • or use boost::thread_group.

Upvotes: 24

Related Questions