Author Topic: std::vector<std::thread> example from Concurrency in action  (Read 127679 times)

Kenneth Carter

  • Beta Testers
  • Newbie
  • *
  • Posts: 18
    • View Profile
std::vector<std::thread> example from Concurrency in action
« 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

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: std::vector<std::thread> example from Concurrency in action
« Reply #1 on: November 21, 2008, 10:43:11 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

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), and the standard library containers will have to support such types. For now, we have to write our own.

Quote
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.

Kenneth Carter

  • Beta Testers
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: std::vector<std::thread> example from Concurrency in action
« Reply #2 on: November 22, 2008, 02:21:27 AM »
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;
    }
« Last Edit: November 22, 2008, 02:23:24 AM by Kenneth Carter »

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: std::vector<std::thread> example from Concurrency in action
« Reply #3 on: November 24, 2008, 08:56:52 AM »
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.

Kenneth Carter

  • Beta Testers
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: std::vector<std::thread> example from Concurrency in action
« Reply #4 on: November 25, 2008, 06:01:13 PM »
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

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: std::vector<std::thread> example from Concurrency in action
« Reply #5 on: November 25, 2008, 06:06:59 PM »
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.

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: std::vector<std::thread> example from Concurrency in action
« Reply #6 on: November 28, 2008, 05:55:22 PM »
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.

Kenneth Carter

  • Beta Testers
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: std::vector<std::thread> example from Concurrency in action
« Reply #7 on: November 28, 2008, 06:48:04 PM »
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

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: std::vector<std::thread> example from Concurrency in action
« Reply #8 on: November 28, 2008, 09:52:11 PM »
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. 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.

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: std::vector<std::thread> example from Concurrency in action
« Reply #9 on: December 01, 2008, 02:45:27 PM »
Support for std::vector<std::thread> will be in the 0.6 beta later this week.