简体   繁体   English

如何将boost线程添加到矢量中

[英]How can I add boost threads to a vector

I have something like this that is incorect: 我有这样的东西是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 ? 也许我应该在向量中添加指向boost :: thread的指针,但后来我不知道如何添加动态分配的线程,我该怎么做才能使这个工作?

Thank you. 谢谢。

  • You must have a compiler with move-semantics supported in order to make your code work, 您必须拥有支持move-semantics的编译器才能使代码正常工作,
  • or use vector<shared_ptr<boost::thread>> with code like: 或使用vector<shared_ptr<boost::thread>>代码如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM