How to use std::async on a queue of vectors or a vector of vectors to sort?
I am trying to compare std::thread
with std::async
in this small example that I came up with for a fun exercise. I generate eight vectors with ten random numbers from 0 to 1000. I then push those vectors into a queue so that I can go ahead and multithread the sorting later. Here is the function I am using to generate the random numbers:
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
In my thread example, I simply lock the queue, grab a vector out of it, call std::sort
on it, and push that back into a list of vectors so I can merge it into one final vector in the end. That was easy enough to implement, but I am trying to figure out how to implement this same thing with std::async
This is what I have tried so far:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
std::vector<int> temp;
temp = queue.front();
auto handle = std::async(std::launch::async,sortIt,temp);
queue.pop_front();
toMerge.push_back(temp);
}
I then realized, that this would not do what I thought it would. As I do not believe this could call itself over and over again. So I tried this one:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
if(queue.empty()
{
return;
}
else
{
auto handle = std::async(std::launch::async[&]{return std::sort(queue.front.begin(),queue.front.end());},work,queue,toMerge);
}
}
But that gave me all sorts of compiler errors that I don't really know how to tackle.
How can I achieve this task?
Full code:
void sortIt(std::vector<int>& v)
{
std::sort(begin(v),end(v));
}
void print(std::vector<int> &v)
{
for(auto &&e : v)
{
std::cout << e << " ";
}
std::cout << std::endl;
}
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
//TODO: Make asnyc work
}
int main()
{
std::deque<std::vector<int>> queue;
std::vector<int> tempA;
std::vector<int> tempB;
std::vector<int> finalVec;
std::list<std::vector<int>> toMerge;
for(int i = 0; i < 8; ++i)
{
queue.push_back(generate(10));
}
work(queue,toMerge);
}
c++ multithreading vector
add a comment |
I am trying to compare std::thread
with std::async
in this small example that I came up with for a fun exercise. I generate eight vectors with ten random numbers from 0 to 1000. I then push those vectors into a queue so that I can go ahead and multithread the sorting later. Here is the function I am using to generate the random numbers:
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
In my thread example, I simply lock the queue, grab a vector out of it, call std::sort
on it, and push that back into a list of vectors so I can merge it into one final vector in the end. That was easy enough to implement, but I am trying to figure out how to implement this same thing with std::async
This is what I have tried so far:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
std::vector<int> temp;
temp = queue.front();
auto handle = std::async(std::launch::async,sortIt,temp);
queue.pop_front();
toMerge.push_back(temp);
}
I then realized, that this would not do what I thought it would. As I do not believe this could call itself over and over again. So I tried this one:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
if(queue.empty()
{
return;
}
else
{
auto handle = std::async(std::launch::async[&]{return std::sort(queue.front.begin(),queue.front.end());},work,queue,toMerge);
}
}
But that gave me all sorts of compiler errors that I don't really know how to tackle.
How can I achieve this task?
Full code:
void sortIt(std::vector<int>& v)
{
std::sort(begin(v),end(v));
}
void print(std::vector<int> &v)
{
for(auto &&e : v)
{
std::cout << e << " ";
}
std::cout << std::endl;
}
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
//TODO: Make asnyc work
}
int main()
{
std::deque<std::vector<int>> queue;
std::vector<int> tempA;
std::vector<int> tempB;
std::vector<int> finalVec;
std::list<std::vector<int>> toMerge;
for(int i = 0; i < 8; ++i)
{
queue.push_back(generate(10));
}
work(queue,toMerge);
}
c++ multithreading vector
It is unclear what you are asking.async
returnsfuture
with results, you need to callget
on future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?
– rafix07
Nov 21 at 8:09
@rafix07 yes that is exactly it. In fact, I do not need async to merge the vectors. I was going to have the program do that in the end by callingstd::merge
on my list of vectors usingtempA
,tempB
, andfinalVec
.
– Sailanarmo
Nov 21 at 16:16
add a comment |
I am trying to compare std::thread
with std::async
in this small example that I came up with for a fun exercise. I generate eight vectors with ten random numbers from 0 to 1000. I then push those vectors into a queue so that I can go ahead and multithread the sorting later. Here is the function I am using to generate the random numbers:
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
In my thread example, I simply lock the queue, grab a vector out of it, call std::sort
on it, and push that back into a list of vectors so I can merge it into one final vector in the end. That was easy enough to implement, but I am trying to figure out how to implement this same thing with std::async
This is what I have tried so far:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
std::vector<int> temp;
temp = queue.front();
auto handle = std::async(std::launch::async,sortIt,temp);
queue.pop_front();
toMerge.push_back(temp);
}
I then realized, that this would not do what I thought it would. As I do not believe this could call itself over and over again. So I tried this one:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
if(queue.empty()
{
return;
}
else
{
auto handle = std::async(std::launch::async[&]{return std::sort(queue.front.begin(),queue.front.end());},work,queue,toMerge);
}
}
But that gave me all sorts of compiler errors that I don't really know how to tackle.
How can I achieve this task?
Full code:
void sortIt(std::vector<int>& v)
{
std::sort(begin(v),end(v));
}
void print(std::vector<int> &v)
{
for(auto &&e : v)
{
std::cout << e << " ";
}
std::cout << std::endl;
}
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
//TODO: Make asnyc work
}
int main()
{
std::deque<std::vector<int>> queue;
std::vector<int> tempA;
std::vector<int> tempB;
std::vector<int> finalVec;
std::list<std::vector<int>> toMerge;
for(int i = 0; i < 8; ++i)
{
queue.push_back(generate(10));
}
work(queue,toMerge);
}
c++ multithreading vector
I am trying to compare std::thread
with std::async
in this small example that I came up with for a fun exercise. I generate eight vectors with ten random numbers from 0 to 1000. I then push those vectors into a queue so that I can go ahead and multithread the sorting later. Here is the function I am using to generate the random numbers:
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
In my thread example, I simply lock the queue, grab a vector out of it, call std::sort
on it, and push that back into a list of vectors so I can merge it into one final vector in the end. That was easy enough to implement, but I am trying to figure out how to implement this same thing with std::async
This is what I have tried so far:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
std::vector<int> temp;
temp = queue.front();
auto handle = std::async(std::launch::async,sortIt,temp);
queue.pop_front();
toMerge.push_back(temp);
}
I then realized, that this would not do what I thought it would. As I do not believe this could call itself over and over again. So I tried this one:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
if(queue.empty()
{
return;
}
else
{
auto handle = std::async(std::launch::async[&]{return std::sort(queue.front.begin(),queue.front.end());},work,queue,toMerge);
}
}
But that gave me all sorts of compiler errors that I don't really know how to tackle.
How can I achieve this task?
Full code:
void sortIt(std::vector<int>& v)
{
std::sort(begin(v),end(v));
}
void print(std::vector<int> &v)
{
for(auto &&e : v)
{
std::cout << e << " ";
}
std::cout << std::endl;
}
std::vector<int> generate(int count)
{
std::vector<int> newVec(count);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0,1000);
std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt)));
return newVec;
}
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge)
{
//TODO: Make asnyc work
}
int main()
{
std::deque<std::vector<int>> queue;
std::vector<int> tempA;
std::vector<int> tempB;
std::vector<int> finalVec;
std::list<std::vector<int>> toMerge;
for(int i = 0; i < 8; ++i)
{
queue.push_back(generate(10));
}
work(queue,toMerge);
}
c++ multithreading vector
c++ multithreading vector
edited Nov 21 at 5:27
asked Nov 21 at 4:50
Sailanarmo
348213
348213
It is unclear what you are asking.async
returnsfuture
with results, you need to callget
on future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?
– rafix07
Nov 21 at 8:09
@rafix07 yes that is exactly it. In fact, I do not need async to merge the vectors. I was going to have the program do that in the end by callingstd::merge
on my list of vectors usingtempA
,tempB
, andfinalVec
.
– Sailanarmo
Nov 21 at 16:16
add a comment |
It is unclear what you are asking.async
returnsfuture
with results, you need to callget
on future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?
– rafix07
Nov 21 at 8:09
@rafix07 yes that is exactly it. In fact, I do not need async to merge the vectors. I was going to have the program do that in the end by callingstd::merge
on my list of vectors usingtempA
,tempB
, andfinalVec
.
– Sailanarmo
Nov 21 at 16:16
It is unclear what you are asking.
async
returns future
with results, you need to call get
on future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?– rafix07
Nov 21 at 8:09
It is unclear what you are asking.
async
returns future
with results, you need to call get
on future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?– rafix07
Nov 21 at 8:09
@rafix07 yes that is exactly it. In fact, I do not need async to merge the vectors. I was going to have the program do that in the end by calling
std::merge
on my list of vectors using tempA
, tempB
, and finalVec
.– Sailanarmo
Nov 21 at 16:16
@rafix07 yes that is exactly it. In fact, I do not need async to merge the vectors. I was going to have the program do that in the end by calling
std::merge
on my list of vectors using tempA
, tempB
, and finalVec
.– Sailanarmo
Nov 21 at 16:16
add a comment |
1 Answer
1
active
oldest
votes
All you need is to create vector for holding future objects,
iterate over queue
and at each iterator call async
function
to start asynchronous operation - in your case it is sorting of vector.
For each future object you have to call get
method to retrieve sorted vector.
Obtained sorted vector by get
is added to toMerge
list.
void work(
const std::deque<std::vector<int>>& queue, // const added - don't modify queue
std::list<std::vector<int>>& toMerge) // pass toMerge by reference to store results
{
std::vector<std::future< std::vector<int> >> tasks;
for (const std::vector<int>& v : queue)
tasks.push_back (std::async(
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}));
// wait until all tasks are complete
for (std::future< std::vector<int> >& f : tasks)
toMerge.push_back(f.get()); // move vector into list
}
Lambda which sorts look like:
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}
you don't want to modify v
from queue
hence the copy of v
is made,
the content of v
is copied into vecCopy
. Because closure
keeps vecCopy
by value you need to put mutable keyword to lambda to allow
vecCopy
to be modified.
Wow! That was really cool! This is the solution I was looking for, the question I have for you is aboutvecCopy
, what is going on when you callvecCopy = v
? How doesvecCopy
know that it is a vector?
– Sailanarmo
Nov 21 at 19:49
1
Is works since c++14, in C++11 you can pass variable into closure by reference[&v]
- it doesn't work because you cannot call sort onconst v
, or by value[v] mutable
it also doesn't work becausev
is const. Since C++14 you can name captured value and initialize it,vecCopy = v
means that closure hasvecCopy
as data member and this vector is copy ofv
. You can google lambda capture expressions c++14 to see more details.
– rafix07
Nov 21 at 19:58
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405439%2fhow-to-use-stdasync-on-a-queue-of-vectors-or-a-vector-of-vectors-to-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
All you need is to create vector for holding future objects,
iterate over queue
and at each iterator call async
function
to start asynchronous operation - in your case it is sorting of vector.
For each future object you have to call get
method to retrieve sorted vector.
Obtained sorted vector by get
is added to toMerge
list.
void work(
const std::deque<std::vector<int>>& queue, // const added - don't modify queue
std::list<std::vector<int>>& toMerge) // pass toMerge by reference to store results
{
std::vector<std::future< std::vector<int> >> tasks;
for (const std::vector<int>& v : queue)
tasks.push_back (std::async(
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}));
// wait until all tasks are complete
for (std::future< std::vector<int> >& f : tasks)
toMerge.push_back(f.get()); // move vector into list
}
Lambda which sorts look like:
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}
you don't want to modify v
from queue
hence the copy of v
is made,
the content of v
is copied into vecCopy
. Because closure
keeps vecCopy
by value you need to put mutable keyword to lambda to allow
vecCopy
to be modified.
Wow! That was really cool! This is the solution I was looking for, the question I have for you is aboutvecCopy
, what is going on when you callvecCopy = v
? How doesvecCopy
know that it is a vector?
– Sailanarmo
Nov 21 at 19:49
1
Is works since c++14, in C++11 you can pass variable into closure by reference[&v]
- it doesn't work because you cannot call sort onconst v
, or by value[v] mutable
it also doesn't work becausev
is const. Since C++14 you can name captured value and initialize it,vecCopy = v
means that closure hasvecCopy
as data member and this vector is copy ofv
. You can google lambda capture expressions c++14 to see more details.
– rafix07
Nov 21 at 19:58
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
add a comment |
All you need is to create vector for holding future objects,
iterate over queue
and at each iterator call async
function
to start asynchronous operation - in your case it is sorting of vector.
For each future object you have to call get
method to retrieve sorted vector.
Obtained sorted vector by get
is added to toMerge
list.
void work(
const std::deque<std::vector<int>>& queue, // const added - don't modify queue
std::list<std::vector<int>>& toMerge) // pass toMerge by reference to store results
{
std::vector<std::future< std::vector<int> >> tasks;
for (const std::vector<int>& v : queue)
tasks.push_back (std::async(
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}));
// wait until all tasks are complete
for (std::future< std::vector<int> >& f : tasks)
toMerge.push_back(f.get()); // move vector into list
}
Lambda which sorts look like:
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}
you don't want to modify v
from queue
hence the copy of v
is made,
the content of v
is copied into vecCopy
. Because closure
keeps vecCopy
by value you need to put mutable keyword to lambda to allow
vecCopy
to be modified.
Wow! That was really cool! This is the solution I was looking for, the question I have for you is aboutvecCopy
, what is going on when you callvecCopy = v
? How doesvecCopy
know that it is a vector?
– Sailanarmo
Nov 21 at 19:49
1
Is works since c++14, in C++11 you can pass variable into closure by reference[&v]
- it doesn't work because you cannot call sort onconst v
, or by value[v] mutable
it also doesn't work becausev
is const. Since C++14 you can name captured value and initialize it,vecCopy = v
means that closure hasvecCopy
as data member and this vector is copy ofv
. You can google lambda capture expressions c++14 to see more details.
– rafix07
Nov 21 at 19:58
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
add a comment |
All you need is to create vector for holding future objects,
iterate over queue
and at each iterator call async
function
to start asynchronous operation - in your case it is sorting of vector.
For each future object you have to call get
method to retrieve sorted vector.
Obtained sorted vector by get
is added to toMerge
list.
void work(
const std::deque<std::vector<int>>& queue, // const added - don't modify queue
std::list<std::vector<int>>& toMerge) // pass toMerge by reference to store results
{
std::vector<std::future< std::vector<int> >> tasks;
for (const std::vector<int>& v : queue)
tasks.push_back (std::async(
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}));
// wait until all tasks are complete
for (std::future< std::vector<int> >& f : tasks)
toMerge.push_back(f.get()); // move vector into list
}
Lambda which sorts look like:
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}
you don't want to modify v
from queue
hence the copy of v
is made,
the content of v
is copied into vecCopy
. Because closure
keeps vecCopy
by value you need to put mutable keyword to lambda to allow
vecCopy
to be modified.
All you need is to create vector for holding future objects,
iterate over queue
and at each iterator call async
function
to start asynchronous operation - in your case it is sorting of vector.
For each future object you have to call get
method to retrieve sorted vector.
Obtained sorted vector by get
is added to toMerge
list.
void work(
const std::deque<std::vector<int>>& queue, // const added - don't modify queue
std::list<std::vector<int>>& toMerge) // pass toMerge by reference to store results
{
std::vector<std::future< std::vector<int> >> tasks;
for (const std::vector<int>& v : queue)
tasks.push_back (std::async(
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}));
// wait until all tasks are complete
for (std::future< std::vector<int> >& f : tasks)
toMerge.push_back(f.get()); // move vector into list
}
Lambda which sorts look like:
[vecCopy = v]() mutable { // copy v into vecCopy
std::sort(vecCopy.begin(), vecCopy.end());
return vecCopy;
}
you don't want to modify v
from queue
hence the copy of v
is made,
the content of v
is copied into vecCopy
. Because closure
keeps vecCopy
by value you need to put mutable keyword to lambda to allow
vecCopy
to be modified.
answered Nov 21 at 19:08
rafix07
6,6031613
6,6031613
Wow! That was really cool! This is the solution I was looking for, the question I have for you is aboutvecCopy
, what is going on when you callvecCopy = v
? How doesvecCopy
know that it is a vector?
– Sailanarmo
Nov 21 at 19:49
1
Is works since c++14, in C++11 you can pass variable into closure by reference[&v]
- it doesn't work because you cannot call sort onconst v
, or by value[v] mutable
it also doesn't work becausev
is const. Since C++14 you can name captured value and initialize it,vecCopy = v
means that closure hasvecCopy
as data member and this vector is copy ofv
. You can google lambda capture expressions c++14 to see more details.
– rafix07
Nov 21 at 19:58
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
add a comment |
Wow! That was really cool! This is the solution I was looking for, the question I have for you is aboutvecCopy
, what is going on when you callvecCopy = v
? How doesvecCopy
know that it is a vector?
– Sailanarmo
Nov 21 at 19:49
1
Is works since c++14, in C++11 you can pass variable into closure by reference[&v]
- it doesn't work because you cannot call sort onconst v
, or by value[v] mutable
it also doesn't work becausev
is const. Since C++14 you can name captured value and initialize it,vecCopy = v
means that closure hasvecCopy
as data member and this vector is copy ofv
. You can google lambda capture expressions c++14 to see more details.
– rafix07
Nov 21 at 19:58
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
Wow! That was really cool! This is the solution I was looking for, the question I have for you is about
vecCopy
, what is going on when you call vecCopy = v
? How does vecCopy
know that it is a vector?– Sailanarmo
Nov 21 at 19:49
Wow! That was really cool! This is the solution I was looking for, the question I have for you is about
vecCopy
, what is going on when you call vecCopy = v
? How does vecCopy
know that it is a vector?– Sailanarmo
Nov 21 at 19:49
1
1
Is works since c++14, in C++11 you can pass variable into closure by reference
[&v]
- it doesn't work because you cannot call sort on const v
, or by value [v] mutable
it also doesn't work because v
is const. Since C++14 you can name captured value and initialize it, vecCopy = v
means that closure has vecCopy
as data member and this vector is copy of v
. You can google lambda capture expressions c++14 to see more details.– rafix07
Nov 21 at 19:58
Is works since c++14, in C++11 you can pass variable into closure by reference
[&v]
- it doesn't work because you cannot call sort on const v
, or by value [v] mutable
it also doesn't work because v
is const. Since C++14 you can name captured value and initialize it, vecCopy = v
means that closure has vecCopy
as data member and this vector is copy of v
. You can google lambda capture expressions c++14 to see more details.– rafix07
Nov 21 at 19:58
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
That is super cool! Thank you for your help!
– Sailanarmo
Nov 21 at 20:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405439%2fhow-to-use-stdasync-on-a-queue-of-vectors-or-a-vector-of-vectors-to-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It is unclear what you are asking.
async
returnsfuture
with results, you need to callget
on future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?– rafix07
Nov 21 at 8:09
@rafix07 yes that is exactly it. In fact, I do not need async to merge the vectors. I was going to have the program do that in the end by calling
std::merge
on my list of vectors usingtempA
,tempB
, andfinalVec
.– Sailanarmo
Nov 21 at 16:16