C + + thread operator= rvalue reference vector foreach

Source: Internet
Author: User

This is the construct definition of thread:

Default (1)
Thread () noexcept;
Initialization (2)
Template <class Fn, class ... Args>explicit Thread (fn&& Fn, Args&& .... Args);
copy [deleted] (3)
Thread (const thread&) = delete;
Move (4)
Thread (thread&& x) noexcept;

You can see that the thread is not copy, and the definition of his = operator:

Move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;

Only rvalue references (http://stackoverflow.com/questions/3106110/what-are-move-semantics) are allowed, so if you want to vector<thread> you must write this:

std::vector<std::thread> threads;  for (int i=1; i<=10; ++i)    threads.push_back(std::thread(increase_global,1000));   
//分开写: thread t()
// thread.push_back(t) 就无法编译通过,因为要用到operator= ,而 thread只允许move semnantics,就是只允许右值引用


{
auto && __range = range_expression ;
for (auto __begin = begin_expr ,
__end = end_expr ;
range_declaratio
__begin != __end; ++__begin) {
n = *__begin; //need operator= here
Loop_statement

So range for is also not available for thread



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.