Why can't I create a vector of threads on the fly like this

cpp_hex

Why is creation of vector of threads on the fly is wrong ? I am getting compilation error

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(593): error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function

followed by a lot of other stuff.

#include <iostream>
#include <thread>
#include <vector>

using std::vector;
using std::thread;
using std::cout;

class obj_on_thread {
public:
    void operator()()
    {
        std::cout << "obj on thread\n";
    }
};

void function_on_thread() {
    std::cout << "function on thread\n";
}

auto named_lambda = []() { std::cout << "named_lambda_on_thread\n"; };

int main(){
    obj_on_thread obj;

    vector<thread> pool {
        thread{ obj },
        thread{ function_on_thread },
        thread{ named_lambda },
        thread{ []() { cout << "anonymous lambda on thread\n"; } }
    };
    cout << "main thread\n";

    for(auto& t : pool)
    {
        if (t.joinable())
        {
            cout << "Joinable = true";
            t.join(); //if called must be called once.
        }
        else
        {
            cout << "this shouldn't get printed, joinable = false\n";
        }
    }
    for (auto& t : pool)
    {
        if (t.joinable())
        {
            cout << " This won't be printed Joinable = true";
        }
        else
        {
            cout << "joinable = false thread are already joint\n";
        }
    }

    return 0;
}
Freddie Chopin

std::vector's constructor which uses initializer_list requires the elements to be copy-constructible, because initializer_list itself requires that (its underlying "storage" is implemented as a temporary array which is copy-constructed). std::thread is not copy-constructible (this constructor is deleted).

http://en.cppreference.com/w/cpp/utility/initializer_list

The underlying array is a temporary array, in which each element is copy-initialized...

There's no good way to solve this problem - you cannot initialize all your threads at once, but you can use (multiple calls, one for each thread - unfortunately):

  • emplace_back():

    pool.emplace_back(obj);
    
  • push_back() with rvalues:

    pool.push_back(thread{obj});
    
  • push_back() with explicit move()s:

    auto t = thread{obj};
    pool.push_back(std::move(t));
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create and write to vector on the fly

From Dev

Create and write to vector on the fly

From Dev

Threads in a vector can't be joined

From Dev

Why can't I change objects in a vector?

From Dev

why can't I initialize the array like this?

From Dev

Why can't I wrap a T* in an std::vector<T>?

From Dev

Why can't I create the Java list?

From Dev

Why can't I create a new Color?

From Dev

Why can't I create a database in MongoDB

From Dev

Why can't I create a Maven project?

From Dev

why i can't create view in sqlite?

From Dev

Why can't I create the Java list?

From Dev

Why can't I create a Jenkins account?

From Dev

Why I can't create a new partition?

From Dev

Why can't I create a database in MongoDB

From Dev

Why can't I create an NSMutableArray of integers?

From Dev

Why can't I create a Maven project?

From Dev

Why can't I create a physical volume?

From Dev

Why can't I create an extended partition?

From Dev

Why can't I add items into my vector?

From Dev

Why can't I convert a vector into a matrix with 'as.matrix' function?

From Dev

Why can't I access directly in vector iterator?

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Why can't i construct a vector by passing temporary input iterator?

From Dev

Why I can't see any ouput iterating this vector?

From Dev

Why can't I set an iterator on a list in a vector?

From Dev

Why I can't desalinize std::array like this?

From Dev

Why can't I overwrite the value of a variable like this?

From Dev

Why I can't get Symfony Finder like a service?