just::thread Support Forum
General Category => General Discussion about just::thread => Topic started by: Kenneth Carter on November 21, 2008, 06:46:43 PM
-
Error 4
error C2558: class 'std::thread' : no copy constructor available or copy constructor is declared 'explicit'
f:\program files (x86)\microsoft visual studio 9.0\vc\include\vector line 1211
I recently aquired your book and was reading through. and I tried using a std::vector with the just::thread class.
Kenneth
-
Error 4
error C2558: class 'std::thread' : no copy constructor available or copy constructor is declared 'explicit'
f:\program files (x86)\microsoft visual studio 9.0\vc\include\vector line 1211
std::thread is movable, but not copyable. This means that you need a move-aware container to be able to store it --- none of the containers shipped with MSVC 2008 are move aware, because every movable-but-not-copyable type does it it's own way. In C++0x move semantics is supported with rvalue references (see my blog entry at http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html (http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html)), and the standard library containers will have to support such types. For now, we have to write our own.
I recently aquired your book and was reading through. and I tried using a std::vector with the just::thread class.
Thanks for reading. Not all the examples can be made to work, unfortunately.
-
Does the below code satisfy the condition with vector? it compiles but I have not executed the code at this time.
std::vector<std::thread> threads( num_threads - 1 );
std::vector<T> results( num_threads );
// iterate the thread array and send chunks of data into the threads
iterator block_start = first;
for( unsigned long i = 0; i < ( num_threads - 1 ); ++i )
{
iterator block_end = block_start;
std::advance(block_end, block_size);
threads = std::move( std::thread( //<-- this compiles
accumulate_block<iterator, T>(),
block_start,
block_end,
std::ref( results ) ) );
block_start = block_end;
}
-
Your example looks like it should work. Only functions that resize a vector or otherwise copy elements require the contained type to be copyable (so that's resize, push_back, erase, insert, copy-construction and copy assignment of the vector, swap and reserve).
If you know the size of your vector up front then you can pre-allocate it and use move-assignment, as in your example. My previous blanket statement was a bit of a generalization.
-
the above sample would still not work. what I have found that works if I use a raw array pointer of std::thread *ptr_threads = new std::thread[ num_threads - 1 ].
the only draw back is that I have to remember to clean it up with delete [] ptr_threads.
Kenneth
-
the above sample would still not work. what I have found that works if I use a raw array pointer of std::thread *ptr_threads = new std::thread[ num_threads - 1 ].
the only draw back is that I have to remember to clean it up with delete [] ptr_threads.
Yes, I tried it. The code that requires the copy constructor is in the allocator, and is not actually used in this example, but it is required to compile. I'll see if I can figure out a way to write a custom allocator that allows this to work.
-
The code that requires the copy constructor is in the allocator, and is not actually used in this example, but it is required to compile. I'll see if I can figure out a way to write a custom allocator that allows this to work.
With a small amount of code, it is possible to make std::vector<std::thread> work for fixed-sized vectors, but you cannot call resize(), push_back(), erase(), insert() or clear(). You can't even call swap(). I'm not sure it's worth it - what we really need is a move-aware vector implementation.
-
I would agree with you on this point. so what we would need is a move aware vector list. can you post links to the necesary information so if i were to persue development of a move aware container like vector or a custom array class. I would at least have the information to do so.
Kenneth
-
I would agree with you on this point. so what we would need is a move aware vector list. can you post links to the necesary information so if i were to persue development of a move aware container like vector or a custom array class. I would at least have the information to do so.
The C++0x working draft is at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf). std::vector is described in chapter 23.2.6.
In order to support my implementation of std::thread (and other movable types such as std::unique_future<> and std::packaged_task<>) on MSVC 2008, you will need to handle types that convert to __jss::move_emulation_t<T> differently from "normal" types and move them rather than copy them.
-
Support for std::vector<std::thread> will be in the 0.6 beta later this week.