0

What I want to do is --> create a new object in a new thread. Something like:

Class* object = 0;
Arg arg;
boost::thread t( lambda::bind( object = lambda::new_ptr< Class >()( boost::ref( arg ) );

it doesn't compile, what's correct way?

3
  • 2
    Hint: Use Boost.Phoenix rather than Boost.Lambda – the latter has been deprecated for years now.
    – ildjarn
    Commented Feb 19, 2013 at 1:19
  • 1
    If C++11 is an option for you, just do std::thread t([&] { object = new Class(arg); } );
    – Andy Prowl
    Commented Feb 19, 2013 at 1:38
  • Thanks guys, but I can't use C++11 right now. I'm going to try Phoenix...
    – JQ.
    Commented Feb 19, 2013 at 3:37

1 Answer 1

0

Thanks to ildjarn, I tried with boost::phoenix and got it work, code is:

Class* object = 0;
CArg arg0;
Arg arg1;

boost::thread t( boost::phoenix::val( boost::ref( object ) ) = boost::phoenix::new_< Class >( boost::cref( arg0 ), boost::ref( arg1 ) );

Again, from ildjarn, the better code is:

Class* object = 0;

CArg arg0;

Arg arg1;

namespace phx = boost::phoenix;

boost::thread t( phx::ref( object ) = phx::new_< Class >( phx::cref( arg0 ), phx::ref( arg1 ) );

1
  • 1
    This should be boost::thread t( phx::ref( object ) = phx::new_< Class >( phx::cref( arg0 ), phx::ref( arg1 ) ); (given namespace phx = boost::phoenix;). Notably, you should be using ref/cref from namespace boost::phoenix rather than namespace boost.
    – ildjarn
    Commented Feb 20, 2013 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.