Boost logo

Boost :

Subject: [boost] [thread] using Boost.Thread with move-only arguments
From: Fernando Pelliccioni (fpelliccioni_at_[hidden])
Date: 2013-04-03 22:40:35


Hi Vicente,

I have an issue with Boost.Thread contructor using move-only type arguments.

The "Thread Constructor with arguments" section of the documentation says
...

template <class F,class A1,class A2,...>
thread(F f,A1 a1,A2 a2,...);

Preconditions:
F and each An must by copyable or movable.

http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#thread.thread_management.thread.multiple_argument_constructor

"An" could be copyable or movable, but the following example fails to
compile...

error: use of deleted function 'A::A(const A&)'

//Begin code ------------------------------------
#include <boost/thread.hpp>

struct A
{
    A() = default;
    A( const A& other) = delete;
    A& operator=( const A& other) = delete;
    A( A&& other) = default;
    A& operator=( A&& other) = default;

    void run() {}
};

void f( A a )
{
    a.run();
}

void exec_boost()
{
    A a;
    boost::thread t( f, std::move(a) );
    t.join();
}

//End code --------------------------------------

I think we should modify ...

    template <class F,class A1>
    thread(F f,A1 a1,typename
disable_if<boost::thread_detail::is_convertible<F&,thread_attributes >,
dummy* >::type=0):
        thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1)))
    {
        start_thread();
    }

... by ...

    template <class F,class A1>
    thread(F&& f,A1&& a1,typename
disable_if<boost::thread_detail::is_convertible<F&,thread_attributes >,
dummy* >::type=0):

thread_info(make_thread_info(boost::bind(boost::type<void>(),f,boost::forward<A1>(a1))))
    {
        start_thread();
    }

( In the all family of related ctors in "boost/thread/detail/thread.hpp" )

But, I don't know if boost::bind is forwarding its arguments
I think we should modify Boost.Bind to be similar to std::bind.
Is there any reason why Bind has not changed so far? Have you found any
limitations?

If Bind can not be changed, I think Boost.Thread should use some other tool
similar to Bind to make forwarding parameters.
GCC Libstdc++ use a Bind-lite tool for std::thread.

I can work on this if you want.
What do you think?

Environment:
Windows 7 64bits
MinGW - GCC 4.7.2
MSVC 2010
MSVC 2012
Boost 1.53.0
Non-Variadic template code branch

Regards,
Fernando Pelliccioni


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk