boost::future and continuations - value set, but future still blocks - c++

I am trying to make the following continuation work - but f.get() blocks. Whats wrong?
#include <iostream>
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <boost/thread/future.hpp>
struct Foo {
boost::future<int> start() {
return p.get_future();
}
void finish() {
p.set_value(23);
}
boost::promise<int> p;
};
int main () {
Foo foo;
foo.start().then([](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
foo.finish();
}
It'll print the "done:", so the future fires, but it'll then just "hang" on f.get() .. I am lost.
To build:
clang++ -o test8 -std=c++11 -stdlib=libc++ -lboost_thread -lboost_system \
-I/home/oberstet/boost_1_55_0 -L/home/oberstet/boost_1_55_0/stage/lib \
test8.cpp
UPDATE: The following code change will make the example work - but why? Since f2 isn't used anyway. Puzzled again.
boost::future<void> f2 = foo.start().then([](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
UPDATE 2: The following, adding a launch policy launch::deferred, will also work:
foo.start().then(boost::launch::deferred, [](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
and this also:
boost::future<int> start() {
boost::future<int> f = p.get_future();
f.set_deferred();
return f;
}

The problem is, your composed future is not kept around. In fact, it is a temporary and it gets destructed as soon as the statement (with .then()) ends.
Fix it:
int main () {
Foo foo;
auto f1 = foo.start();
auto f2 = f1.then([](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
foo.finish();
f2.get();
}
Now it prints
done:
23
See it Live On Coliru
If you move the f2.get() before the foo.finish() it will dead lock again.

Related

How can a simple std::cout << "---" << std::endl; alter how UB manifests?

Some background
Today I saw, in the body of a function kaboom, a local shared_ptr pointing to a *global object and, being captured by reference and its pointee returned by reference by a lambda, i.e. [&local]() -> auto& { return *local; }; this lambda was stored somehow for further use after the function kaboom returned.
As soon as I saw this, I thought the code was invoking undefined behavior. I tried to make a minimal example to support my claim, and came up with the following.
#include <iostream>
#include <memory>
#include <functional>
#include <vector>
struct Resource {
const int i{3};
};
std::shared_ptr<Resource> global = std::make_unique<Resource>();
auto getSharedStuff() {
return global;
}
std::vector<std::function<Resource&()>> actions{};
void kaboom() {
std::shared_ptr<Resource> local = getSharedStuff();
actions.push_back([&local]() -> auto& { return *local; });
}
int main() {
kaboom();
std::cout << global->i << std::endl;
std::cout << actions[0]().i << std::endl;
}
The simple fact that the 2 couts give 3 and 0 respectively is proof for me that I'm observing UB (3 is correct, and 0 could have been anything, including 3, but I've been lucky that it was not 3, so the UB is well manifest).
Good.
The code I'm curious about
But before getting there, an intermediate version of the repro above was this:
#include <iostream>
#include <memory>
#include <functional>
#include <vector>
struct Resource {
Resource(Resource const&) = delete;
Resource(Resource&&) = delete;
Resource() { std::cout << this << "'s ctor" << std::endl; }
~Resource() { std::cout << this << "'s dtor" << std::endl; }
void operator()() { std::cout << this << "'s operator()" << std::endl; }
};
std::shared_ptr<Resource> global = std::make_unique<Resource>();
auto getSharedStuff() {
return global;
}
std::vector<std::function<Resource&()>> actions{};
void kaboom() {
std::shared_ptr<Resource> local = getSharedStuff();
actions.push_back([&local]() -> auto& { return *local; });
}
int main() {
kaboom();
std::cout << "---" << std::endl;
actions[0]()();
}
which can result in this output
0xc602b0's ctor
---
0x7f0f48f7f4a0's operator()
0xc602b0's dtor
I'm kind of ok with this, as actions[0]()() is dereferencing a destroyed shared_ptr and calling operator() on the screwed up result, so I accept that this can be screwup too.
What's funny, though, is that removing std::cout << "---" << std::endl; makes UB less manifest, as the output becomes something like this:
0xce92b0's ctor
0xce92b0's operator()
0xce92b0's dtor
So my question is: how can a "simple" line as std::cout << "---" << std::endl; affect this way the manifestation of UB?

using std::launch::deferred does not defer the function in std::async

https://godbolt.org/z/MEsandWGe
Here's the code I'm testing, same as the above godbolt link:
#include <future>
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
int main() {
int v = 0;
auto a1 = std::async(
std::launch::async | std::launch::deferred, [&v]() {
std::cout << "begin 1" << std::endl;
std::cout << "v = " << v << std::endl;
std::this_thread::sleep_for(100ms);
std::cout << "end 1" << std::endl;
});
auto a2 = std::async(
std::launch::async | std::launch::deferred, [&v]() {
std::cout << "begin 2" << std::endl;
std::this_thread::sleep_for(200ms);
v = 123;
std::cout << "end 2" << std::endl;
});
a2.wait();
// My understanding is a1 does not run until here?
// thus v should print 123 instead of 0 ?
a1.wait();
return 0;
}
If I remove the async policy, it works as expected.
The question is, why do I see a1 running concurrently with a2 and see v printed as 0? Does the async policy affect the behavior of deferred?
std::launch::async | std::launch::deferred means that std::async can choose which of the two policies is selected. It is implementation defined which one is used but it looks like your standard library chooses async rather than deferred.

std::jthread runs a member function from another member function

Here is my code:
#include <iostream>
#include <zconf.h>
#include <thread>
class JT {
public:
std::jthread j1;
JT() {
j1 = std::jthread(&JT::init, this, std::stop_token());
}
void init(std::stop_token st={}) {
while (!st.stop_requested()) {
std::cout << "Hello" << std::endl;
sleep(1);
}
std::cout << "Bye" << std::endl;
}
};
void init_2(std::stop_token st = {}) {
while (!st.stop_requested()) {
std::cout << "Hello 2" << std::endl;
sleep(1);
}
std::cout << "Bye 2" << std::endl;
}
int main() {
std::cout << "Start" << std::endl;
JT *jt = new JT();
std::jthread j2(init_2);
sleep(5);
std::cout << "Finish" << std::endl;
}
Here is the output:
Start
Hello
Hello 2
Hello
Hello 2
Hello
Hello 2
Hello
Hello 2
Hello
Hello 2
Finish
Bye 2
Hello
The problem is I could get Bye 2 message but not Bye message.
I know the passed stop_token variable results in this problem but I do not know how to pass it to a member function inside another member function.
If I'm understanding the problem correctly (my understanding being that for std::jthread(&JT::init, this) jthread wants to call JT::init(std::stop_token st, this), which isn't going to work), you probably want to use std::bind_front to give it a Callable that works.
e.g.
JT() {
j1 = std::jthread(std::bind_front(&JT::init, this));
}
According to the useful comments, I have rewritten the class code as below:
class JT {
public:
std::jthread j1;
JT() {
j1 = std::jthread(&JT::init, this);
}
void init() {
auto st = j1.get_stop_token();
while (!st.stop_requested()) {
std::cout << "Hello" << std::endl;
sleep(1);
}
std::cout << "Bye" << std::endl;
}
};
You must get the stop_token on the fly through auto st = j1.get_stop_token();.
And the revised main function:
int main() {
std::cout << "Start" << std::endl;
JT *jt = new JT();
// auto jt = std::make_unique<JT>();
std::jthread j2(init_2);
sleep(5);
std::cout << "Finish" << std::endl;
delete jt;
}
You need to delete the class object directly or use RAII (like smart pointers).
The std::stop_token must be received as parameter by the JT::init function, during the thread construction. You can use either std::bind
j1 = std::jthread{ std::bind(&JT::init, this, std::placeholders::_1) };
or, more simpler, std::bind_front as in #Hasturkun answer.
Note
Obtaining the std::stop_token after the thread has been constructed will eventually result in missing the stop request, as demonstrated bellow:
#include <thread>
#include <iostream>
using namespace std::chrono_literals;
class JT {
public:
std::jthread j1;
JT() {
j1 = std::jthread(&JT::init, this);
}
~JT() {
j1.request_stop();
j1.join();
}
void init() {
auto st = j1.get_stop_token();
while (!st.stop_requested()) {
std::this_thread::sleep_for(1ms);
std::cout << "Hello" << std::endl;
}
std::cout << "Bye" << std::endl;
}
};
int main() {
std::cout << "Start" << std::endl;
for (int i = 0; i < 1000; i++) {
JT jt;
std::this_thread::sleep_for(5ms);
}
}
Which results in:
Start
Hello
Bye
Hello
Bye
Hello
Hello
Hello
Hello
Hello
Hello
....
and program never ending. I've tested on release with gcc 12.1.0 and msvc (VS 2019 16.11.5).

Are boost::bind and boost::function compatible with SEH?

I have a function that uses boost::bind to move function evaluation into a try/catch wrapper, based on this question.
Problem is, boost::bind doesn't seem to work with SEH - and worse, it returns a garbage value, which is exactly what I don't want. Have I made a botch of it somewhere? Even stranger, the switch /EHsc or /EHa doesn't actually seem to matter to the behavior of the program. The output you get is:
Calling
-2147483648
Done.
Press any key to continue . . .
How do I even start to figure out where things have gone wrong?
#pragma once
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <windows.h>
#include <eh.h>
using boost::function;
using boost::bind;
int potato(int q)
{
double r = 0;
return q / r;
}
template<typename T>
T exception_wrapper(boost::function<T()> func)
{
try
{
std::cout << "Calling" << std::endl;
return func();
std::cout << "After Call" << std::endl;
}
catch (...) {
std::cout << "Ex Caught" << std::endl;
return 45;
}
}
void trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
{
throw std::runtime_error("huh");
}
int main()
{
int result = exception_wrapper<int>(bind(potato, 123));
std::cout << result << std::endl;
std::cout << "Done." << std::endl;
}

std::async doesn't parallelize tasks

Using C++11 std::async in this snippet:
int foo()
{
::sleep(2);
return 123;
}
int main()
{
future<int> r1(async(foo));
int r2 = foo();
cout << r1.get() + r2 << endl;
return 0;
}
It produces the right result, but runs both foo's serially (whole app runs 4 seconds). Compiled as:
g++ -std=gnu++11 -O2 foo.cc -lpthread (Ubuntu 12.10 64bit, gcc 4.7.2)
You might need to add a launch policy of std::launch::async:
std::async(std::launch::async, foo);
std::future has been criticized e.g. in this CppCon presentation for being slow. You can avoid std::async and std:future entirely by using this header-only library. You can run any number of functions asynchronously and get the results as a tuple. Also exceptions can be caught normally.
Here is an example:
#include <iostream>
#include "Lazy.h"
template <class T>
T foo(T x) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
return x + 10.5;
}
int main() {
int input = 54;
try {
auto [i, d, c] = Lazy::runParallel(
[&](){ return foo(int(input)); },
[&](){ return foo(double(input)); },
[&](){ return foo(char(input)); } );
std::cout << "foo(int) = " << i << ", foo(double) = " << d << ", foo(char) = " << c << '\n';
}
catch (...) {
// Deal with the exception here
}
}
/* Output:
foo(int) = 64, foo(double) = 64.5, foo(char) = #
*/