Create Threads in a loop

后端 未结 5 1293
难免孤独
难免孤独 2021-01-14 02:08

I just tested something like this:

boost::thread workerThread1(boost::bind(&Class::Function, this, ...);
boost::thread workerThread2(boost::bind(&Cla         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-14 03:04

    Why don't you use boost::thread_group? You can create/add/remove threads and join them all (boost::thread_group::join_all()).

    boost::thread_group tgroup;
    for(...)
    {
      tgroup.create_thread(boost::bind(&Class::Function, this, ...)) ;
    }
    tgroup.join_all();
    

    But be careful about the number threads you are creating, too many threads may lead to OutOfMemory.

提交回复
热议问题