
Hi, I am creating "n" number of thread in a loop like this. for(int i=0;i<n;++i) { boost::shared_ptr<boost::thread> th(new boost::thread(boost::bind(&Enum::Execute,shared_from_this(),i,x))); } How can I join all these threads? The approach I used is given below... Before the loop I have created a thread_group object boost::thread_group tg; for(int i=0;i<n;++i) { boost::shared_ptr<boost::thread> th(new boost::thread(boost::bind(&Enum::Execute,shared_from_this(),i,x))); tg.add_thread(th.get()); } tg.join_all(); But the threads are not joined, it is exiting without completion. What could be the mistake I am doing? Thanks, Lloyd

Lloyd wrote:
boost::thread_group tg; for(int i=0;i<n;++i) { boost::shared_ptr<boost::thread> th(new boost::thread(boost::bind(&Enum::Execute,shared_from_this(),i,x))); tg.add_thread(th.get()); } tg.join_all();
But the threads are not joined, it is exiting without completion. What could be the mistake I am doing?
The thread pointer (th) will be destroyed once it goes out of scope, right after end-curly-bracket in the loop. I think this is what you are looking for: boost::thread_group tg; for(int i=0;i<n;++i) { tg.create_thread(boost::bind(&Enum::Execute,shared_from_this(),i,x)); } tg.join_all(); -- Best regards, Martin Dyring-Andersen

Thanks, It worked. regards, Lloyd On Mon, Nov 29, 2010 at 4:36 PM, Martin Dyring-Andersen <mda@spamfighter.com
wrote:
Lloyd wrote:
boost::thread_group tg; for(int i=0;i<n;++i) { boost::shared_ptr<boost::thread> th(new boost::thread(boost::bind(&Enum::Execute,shared_from_this(),i,x))); tg.add_thread(th.get()); } tg.join_all();
But the threads are not joined, it is exiting without completion. What could be the mistake I am doing?
The thread pointer (th) will be destroyed once it goes out of scope, right after end-curly-bracket in the loop.
I think this is what you are looking for:
boost::thread_group tg; for(int i=0;i<n;++i) { tg.create_thread(boost::bind(&Enum::Execute,shared_from_this(),i,x)); } tg.join_all();
-- Best regards, Martin Dyring-Andersen _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Lloyd
-
Lloyd Kanakkassery
-
Martin Dyring-Andersen