Create Threads in a loop

后端 未结 5 1291
难免孤独
难免孤独 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 02:50

    Can you not just create a list (or similar) of threads and then just create them and add to the list.

    Something like the following (which is likely more pseudo code that anything :-) )

    list threads;
    
    for
    {
        boost::thread* name = new boost::thread(...);
        threads.push_back(name);
    }
    

    As mentioned in another answer you can use smart pointers which would be better and you mentioned you have a defined number of threads so an array/vector would be a better choice but as I said the code above isn't perfect anyway

提交回复
热议问题