
I have this situation: void my_thread(std::string const& a, std::string const& b) { } void spawner() { boost::thread(boost::function<void (std::string const&, std::string const&>(my_thread), "bla", "lala"); } This works, but is it possible that the string temporaries are destroyed before the thread starts running?

El 04/03/2010 10:03 p.m., anony escribió:
I have this situation:
void my_thread(std::string const& a, std::string const& b) { }
void spawner() { boost::thread(boost::function<void (std::string const&, std::string const&>(my_thread), "bla", "lala"); }
This works, but is it possible that the string temporaries are destroyed before the thread starts running?
Extract from Boost.Thread documentation at http://www.boost.org/doc/libs/1_42_0/doc/html/thread/thread_management.html#... "Thread Constructor with arguments template <class F,class A1,class A2,...> thread(F f,A1 a1,A2 a2,...); Effects: As if thread(boost::bind(f,a1,a2,...)). Consequently, f and each an are copied into internal storage for access by the new thread." So whether the string temporaries are destroyed is irrelevant; my_thread function will be called with *copies* of those temporaries. Agustín K-ballo Bergé.- http://talesofcpp.blogspot.com

El 05/03/2010 12:18 a.m., Agustín K-ballo Bergé escribió:
El 04/03/2010 10:03 p.m., anony escribió:
I have this situation: void spawner() { boost::thread(boost::function<void (std::string const&, std::string const&>(my_thread), "bla", "lala"); }
By the way, I think the use of boost::function is redundant. Doesn't it just work like this: boost::thread(my_thread, "bla", "lala"); Agustín K-ballo Bergé.- http://talesofcpp.blogspot.com

By the way, I think the use of boost::function is redundant. Doesn't it just work like this:
boost::thread(my_thread, "bla", "lala");
I wanted to give a simple example; in reality my_thread() is a method and I use something like: boost::thread(boost::function<void (SomeClass*, std::string const&, std::string const&)>(&SomeClass::my_thread, this, "bla", "lala"); Is the use of Boost.Function also redundant in this case? I could use either Boost.Bind or Boost.Lambda, but I think Boost.Function is more lightweight than either. Can I instantiate boost::function with an object reference rather than a an object pointer?

The use of Boost.Function is irrelevant. I've checked Boost.Thread source code and it uses Boost.Bind internally. The use of Boost.Function actually increases code size, even though everything compiles and works.
participants (2)
-
Agustín K-ballo Bergé
-
anony