Convert concurrent_vector to std :: vector

I am looking for a recommended way to convert concurrent_vectorfrom a PPL library to a regular std :: vector.

I have a function that returns its results in std::vector, but may or may not use parallel algorithms inside. I am currently using insertto copy elements from a parallel vector to a normal vector:

#ifdef PARALLEL
    std::vector<Foo> Bar() {
        concurrency::concurrent_vector<Foo> cv;

        //Fill cv in a parallel algorithm

        std::vector<Foo> sv;
        sv.insert(sv.begin(), cv.begin(), cv.end());
        return sv;
    }
#else
    std::vector<Foo> Bar() {
        std::vector<Foo> sv;

        //Fill sv in a sequential algorithm

        return sv;
    }
#endif

Although the performance of the insert operation is not real output at the moment (compared to the body of the function), it just seems unnecessary, and I wonder if there is a better solution (Btw .: Foois a simple POD that cannot be moved).

Ideally, I would like to have something like this

std::vector<Foo> Bar() {
    concurrency::concurrent_vector<Foo> cv;

    //Fill cv in a parallel algorithm

    return static_cast<std::vector<Foo>>(cv);
}

or at least

std::vector<Foo> Bar() {
    concurrency::concurrent_vector<Foo> cv;

    //Fill cv in a parallel algorithm

    std::vector<Foo> sv(std::move(cv));
    return sv;
}

Are there any suggestions on how to do this correctly?


EDIT:
, ( ):

std::vector<Foo> Bar() {
    concurrency::concurrent_vector<Foo> cv;

    //Fill cv in a parallel algorithm

    return std::vector<Foo>(cv.begin(), cv.end());
}

( ) , .

EDIT2:

, - , ( std::vector), RVO

+4
2

, - , ( std::vector), RVO ' ?

concurrent_vector std::vector. , , std::vector. , , , , ; .

+2

- std::vector .

/, , , g++, TBB.

0

Source: https://habr.com/ru/post/1543455/


All Articles