Is it ok to destroy a std::promise before future.get() is called?
I was wondering if it is ok to call promise.get_future(), move that future somewhere else (e.g. into a vector) and possibly let the promise die before even the future.get() is called. In the following example, the call gateway->refreshWithCallback
executes the lambda in a thread so that the shared pointer may set the promise free even if in the second loop the future.get() was not called yet, which seems to work but I am angsty!
std::vector<std::future<bool>> futures;
for(GuiGateway *gateway : gateways){
std::shared_ptr<std::promise<bool>> shared_promise_ptr(new std::promise<bool>());
futures.push_back(shared_promise_ptr.get()->get_future());
gateway->refreshWithCallback([shared_promise_ptr](bool success){
shared_promise_ptr.get()->set_value(success);
});
}
qDebug() << "waiting for futures";
for(std::future<bool> &future : futures){
if(future.get() == false){
qDebug() << "error retrieving all gateway data, aborting auto config";
return;
}
}
c++ concurrency promise future
add a comment |
I was wondering if it is ok to call promise.get_future(), move that future somewhere else (e.g. into a vector) and possibly let the promise die before even the future.get() is called. In the following example, the call gateway->refreshWithCallback
executes the lambda in a thread so that the shared pointer may set the promise free even if in the second loop the future.get() was not called yet, which seems to work but I am angsty!
std::vector<std::future<bool>> futures;
for(GuiGateway *gateway : gateways){
std::shared_ptr<std::promise<bool>> shared_promise_ptr(new std::promise<bool>());
futures.push_back(shared_promise_ptr.get()->get_future());
gateway->refreshWithCallback([shared_promise_ptr](bool success){
shared_promise_ptr.get()->set_value(success);
});
}
qDebug() << "waiting for futures";
for(std::future<bool> &future : futures){
if(future.get() == false){
qDebug() << "error retrieving all gateway data, aborting auto config";
return;
}
}
c++ concurrency promise future
5
Note that there existsstd::make_shared
which may be a bit more efficient at creating new objects that will be stored in astd::shared_ptr
(allocates enough memory for the object and the control block in one shot) and covers an edge case regarding exception safety. It also saves on repetition, you can writeauto shared_promise_ptr = std::make_shared<std::promise<bool>>();
.
– François Andrieux
Jan 23 '18 at 18:18
add a comment |
I was wondering if it is ok to call promise.get_future(), move that future somewhere else (e.g. into a vector) and possibly let the promise die before even the future.get() is called. In the following example, the call gateway->refreshWithCallback
executes the lambda in a thread so that the shared pointer may set the promise free even if in the second loop the future.get() was not called yet, which seems to work but I am angsty!
std::vector<std::future<bool>> futures;
for(GuiGateway *gateway : gateways){
std::shared_ptr<std::promise<bool>> shared_promise_ptr(new std::promise<bool>());
futures.push_back(shared_promise_ptr.get()->get_future());
gateway->refreshWithCallback([shared_promise_ptr](bool success){
shared_promise_ptr.get()->set_value(success);
});
}
qDebug() << "waiting for futures";
for(std::future<bool> &future : futures){
if(future.get() == false){
qDebug() << "error retrieving all gateway data, aborting auto config";
return;
}
}
c++ concurrency promise future
I was wondering if it is ok to call promise.get_future(), move that future somewhere else (e.g. into a vector) and possibly let the promise die before even the future.get() is called. In the following example, the call gateway->refreshWithCallback
executes the lambda in a thread so that the shared pointer may set the promise free even if in the second loop the future.get() was not called yet, which seems to work but I am angsty!
std::vector<std::future<bool>> futures;
for(GuiGateway *gateway : gateways){
std::shared_ptr<std::promise<bool>> shared_promise_ptr(new std::promise<bool>());
futures.push_back(shared_promise_ptr.get()->get_future());
gateway->refreshWithCallback([shared_promise_ptr](bool success){
shared_promise_ptr.get()->set_value(success);
});
}
qDebug() << "waiting for futures";
for(std::future<bool> &future : futures){
if(future.get() == false){
qDebug() << "error retrieving all gateway data, aborting auto config";
return;
}
}
c++ concurrency promise future
c++ concurrency promise future
asked Jan 23 '18 at 18:16
Magnus LutzMagnus Lutz
1436
1436
5
Note that there existsstd::make_shared
which may be a bit more efficient at creating new objects that will be stored in astd::shared_ptr
(allocates enough memory for the object and the control block in one shot) and covers an edge case regarding exception safety. It also saves on repetition, you can writeauto shared_promise_ptr = std::make_shared<std::promise<bool>>();
.
– François Andrieux
Jan 23 '18 at 18:18
add a comment |
5
Note that there existsstd::make_shared
which may be a bit more efficient at creating new objects that will be stored in astd::shared_ptr
(allocates enough memory for the object and the control block in one shot) and covers an edge case regarding exception safety. It also saves on repetition, you can writeauto shared_promise_ptr = std::make_shared<std::promise<bool>>();
.
– François Andrieux
Jan 23 '18 at 18:18
5
5
Note that there exists
std::make_shared
which may be a bit more efficient at creating new objects that will be stored in a std::shared_ptr
(allocates enough memory for the object and the control block in one shot) and covers an edge case regarding exception safety. It also saves on repetition, you can write auto shared_promise_ptr = std::make_shared<std::promise<bool>>();
.– François Andrieux
Jan 23 '18 at 18:18
Note that there exists
std::make_shared
which may be a bit more efficient at creating new objects that will be stored in a std::shared_ptr
(allocates enough memory for the object and the control block in one shot) and covers an edge case regarding exception safety. It also saves on repetition, you can write auto shared_promise_ptr = std::make_shared<std::promise<bool>>();
.– François Andrieux
Jan 23 '18 at 18:18
add a comment |
1 Answer
1
active
oldest
votes
If you provide a value to the std::promise
before destroying it, the associated std::future
will be able to retrieve it just fine. It does not depend on std::promise
still existing. If you failed to provide a value to std::promise
before destroying it, trying to get a result from the std::future
will throw std::future_error
but it is also well defined.
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%2f48408318%2fis-it-ok-to-destroy-a-stdpromise-before-future-get-is-called%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
If you provide a value to the std::promise
before destroying it, the associated std::future
will be able to retrieve it just fine. It does not depend on std::promise
still existing. If you failed to provide a value to std::promise
before destroying it, trying to get a result from the std::future
will throw std::future_error
but it is also well defined.
add a comment |
If you provide a value to the std::promise
before destroying it, the associated std::future
will be able to retrieve it just fine. It does not depend on std::promise
still existing. If you failed to provide a value to std::promise
before destroying it, trying to get a result from the std::future
will throw std::future_error
but it is also well defined.
add a comment |
If you provide a value to the std::promise
before destroying it, the associated std::future
will be able to retrieve it just fine. It does not depend on std::promise
still existing. If you failed to provide a value to std::promise
before destroying it, trying to get a result from the std::future
will throw std::future_error
but it is also well defined.
If you provide a value to the std::promise
before destroying it, the associated std::future
will be able to retrieve it just fine. It does not depend on std::promise
still existing. If you failed to provide a value to std::promise
before destroying it, trying to get a result from the std::future
will throw std::future_error
but it is also well defined.
answered Jan 23 '18 at 18:22
François AndrieuxFrançois Andrieux
16k32748
16k32748
add a comment |
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.
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%2f48408318%2fis-it-ok-to-destroy-a-stdpromise-before-future-get-is-called%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
5
Note that there exists
std::make_shared
which may be a bit more efficient at creating new objects that will be stored in astd::shared_ptr
(allocates enough memory for the object and the control block in one shot) and covers an edge case regarding exception safety. It also saves on repetition, you can writeauto shared_promise_ptr = std::make_shared<std::promise<bool>>();
.– François Andrieux
Jan 23 '18 at 18:18