movable threads, locks, and boost::array

I just discovered that while none of my standard containers are "move-aware" in the way needed by boost::thread, I can use boost::array to store threads by simply move-assigning new threads into the elements it contains: boost::array<boost::thread, 100> thread_q; for (int i = 0; i < 100; ++i) thread_q[i] = boost::thread( some_function ); it ain't perfect, but given that the number of cores on any real machine is limited these days, you can do a lot with a fixed-size array of thread. Pretty cool; it sure beats using shared_ptr. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

David Abrahams wrote:
I just discovered that while none of my standard containers are "move-aware" in the way needed by boost::thread, I can use boost::array to store threads by simply move-assigning new threads into the elements it contains:
boost::array<boost::thread, 100> thread_q;
for (int i = 0; i < 100; ++i) thread_q[i] = boost::thread( some_function );
it ain't perfect, but given that the number of cores on any real machine is limited these days, you can do a lot with a fixed-size array of thread. Pretty cool; it sure beats using shared_ptr.
Why doesn't it require std::move or boost::move? I thought this was required to move anything. Looks like there is some sort of conversion operator. -- Sohail Somani http://uint32t.blogspot.com

Sohail Somani <sohail@taggedtype.net> writes:
David Abrahams wrote:
I just discovered that while none of my standard containers are "move-aware" in the way needed by boost::thread, I can use boost::array to store threads by simply move-assigning new threads into the elements it contains:
boost::array<boost::thread, 100> thread_q;
for (int i = 0; i < 100; ++i) thread_q[i] = boost::thread( some_function );
it ain't perfect, but given that the number of cores on any real machine is limited these days, you can do a lot with a fixed-size array of thread. Pretty cool; it sure beats using shared_ptr.
Why doesn't it require std::move or boost::move? I thought this was required to move anything. Looks like there is some sort of conversion operator.
You only need std::move or boost::move to move an lvalue. If you've got an rvalue (such as the temporary in Dave's example), move-assignment "just works" in most cases. In boost 1.35.0 this is done with a move-emulation trick involving conversions to boost::detail::thread_move_t<>, but on trunk it uses rvalue references if the compiler supports it. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Anthony Williams wrote:
Sohail Somani <sohail@taggedtype.net> writes:
David Abrahams wrote: [snip]
thread_q[i] = boost::thread( some_function );
[snip]
Why doesn't it require std::move or boost::move? [snip]
You only need std::move or boost::move to move an lvalue. If you've got an rvalue (such as the temporary in Dave's example), move-assignment "just works" in most cases.
In boost 1.35.0 this is done with a move-emulation trick involving conversions to boost::detail::thread_move_t<>, but on trunk it uses rvalue references if the compiler supports it.
Ok, that makes sense. I skipped class that day ;-) -- Sohail Somani http://uint32t.blogspot.com
participants (3)
-
Anthony Williams
-
David Abrahams
-
Sohail Somani