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) = #
*/
Related
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).
In one of my projects I'm using a small utility function, which takes a Message struct and a lambda function, that modifies this message struct.
Now, I unintentionally passed a lambda without the necessary reference &. It perfectly compiles, but doesn't gave the desired output.
As for me, there should be one of the two following behaviors:
Forgetting to write auto&, but just auto should lead to compilation errors
Writing just auto should be interpreted as auto&.
It is possible to prevent compilation in case of a missing & or even better to interpret auto as auto& automatically?
#include <iostream>
#include <functional>
#include <boost/variant.hpp>
struct Message {
int x;
int y;
};
void changeMessage(Message& m, const std::function<void(Message&)>& messageModifier) {
std::cout << "Message before:" << m.x << " " << m.y << "\n";
messageModifier(m);
std::cout << "Message after:" << m.x << " " << m.y << "\n";
}
int main(int, char**) {
{
std::function<void(int&)> f = [](int&) {};
std::function<void(int)> g = [](int) {};
f = g; // This compiles.
}
{
std::function<void(int&)> f = [](int&) {};
std::function<void(int)> g = [](int) {};
//g = f; // This does not compile. Makes perfect sense.
}
Message m{ 10,20 };
{
changeMessage(m, [](auto m) { m.x++; m.y--; }); // User unintentionally forgot &! Can I prevent this from compilation?
std::cout << "Message outside: " << m.x << " " << m.y << "\n";
}
{
changeMessage(m, [](auto& m) { m.x++; m.y--; });
std::cout << "Message outside: " << m.x << " " << m.y << "\n";
}
}
One way to prevent passing Message by value (and auto itself is never a reference) is to disable copy construction:
struct Message {
Message() = default;
Message(const Message&) = delete;
int x;
int y;
};
Another solution suggested by #L. F. is to check that lambda doesn't accept rvalues:
template<class Fn>
void change_message(Message& m, Fn fn) {
static_assert(!std::is_invocable_v<Fn, Message&&>);
fn(m);
}
How can I launch a member function as a std::async task, which returns a std::tuple.
Sample code:
#include <iostream>
#include <future>
#include <tuple>
#include <numeric>
class Foo {
bool calc;
public:
Foo(bool b) : calc(b)
{}
std::tuple<long, double> calc(std::vector<int> a) {
long sum = 0;
double avg = 0.0;
if ((*this).calc) {
long sum = std::accumulate(a.begin(), a.end(), 0);
double avg = sum / a.size();
}
return std::make_tuple(sum, avg);
}
void call_calc(std::vector<int> i) {
auto handle = std::async(&Foo::calc, this, i);
auto resultTuple = handle.get();
std::cout << "Sum = " << std::get<0>(resultTuple) << " Average = " << std::get<1>(resultTuple) << std::endl;
}
};
int main() {
std::vector<int> a{ 2, 5, 6, 7, 3 };
Foo foo(true);
foo.call_calc(a);
}
In this example, without the member variable, code works fine.
The above code is throwing compilation error for following lines:
auto handle = std::async(&Foo::calc, this, i);
Error: No instance of overloaded function 'std::async' matches the argument list.
std::cout << "Sum = " << std::get<0>(resultTuple) << " Average = " << std::get<1>(resultTuple) << std::endl;
Error: No instance of overloaded function 'std::get' matches the argument list.
The problem seems to be that you have both a data member and a member function named calc. Renaming one solves the issue.
[Live example]
I have question regarding interaction between auto and initializer list. Example code:
#include <iostream>
int main()
{
auto a{ 1 };
auto b = { 1 };
auto c = 1;
std::cout << typeid(a).name() << std::endl;
std::cout << typeid(b).name() << std::endl;
std::cout << typeid(c).name() << std::endl;
return 0;
}
Gives output:
int
class std::initializer_list<int>
int
Which is kind of confusing. I'm posting this question as a followup to this. What should happen? I've did some research and it seems that auto c = 1; is illegal now, and it seems that it works because compilers allow this as a backwards compatibility patch. Does this apply also to auto a{1}?
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.