Could two boost threads with the same name run at the same time?

Hi, all, My code looks like this: ============================================================= // M is a class, m_show() is one of its methods. void test_boost_thread_1(){ M p(88); boost::thread pthd(boost::bind(&M::m_show, &p)); } void test_boost_thread_2(){ M p(99); boost::thread pthd(boost::bind(&M::m_show, &p)); } =============================================================== "test_boost_thread_1()" and "test_boost_thread_2()" are running at the same time ,but I find that the output is wrong. If I changed the thread name "pthd" in "test_boost_thread_()" to other name , not "pthd" , it looks ok. why ? ........ Now , many other threads want to run " test_boost_thread_1() " at the "same" time, How could I make it ok ? Thanks for your time. Best Regards.

Hi, In both functions you bind a method of a local object. The local object might die before the thread has a chance to execute that method! So either you must wait until the thread ends: void test_boost_thread_1(){ M p(88); boost::thread pthd(boost::bind(&M::m_show, &p)); pthd.join();} or you must bind your object by value - then the copy of the original object will be executed. Date: Tue, 20 May 2008 21:05:17 +0800From: hexiay@gmail.comTo: boost-users@lists.boost.orgSubject: [Boost-users] Could two boost threads with the same name run at the same time?Hi, all,My code looks like this:=============================================================// M is a class, m_show() is one of its methods.void test_boost_thread_1(){ M p(88); boost::thread pthd(boost::bind(&M::m_show, &p));}void test_boost_thread_2(){ M p(99); boost::thread pthd(boost::bind(&M::m_show, &p));}==============================================================="test_boost_thread_1()" and "test_boost_thread_2()" are running at the same time ,but I find that the output is wrong.If I changed the thread name "pthd" in "test_boost_thread_()" to other name , not "pthd" , it looks ok. why ? ........Now , many other threads want to run " test_boost_thread_1() " at the "same" time, How could I make it ok ?Thanks for your time.Best Regards. _________________________________________________________________ Discover the new Windows Vista http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE
participants (2)
-
he xiang
-
Igor R.