C++11 Concurrency, Part 4

Bartosz Milewski · 30K views · 2012 · 22:46

Open on YouTube

C, C++, Rust, Go & systems languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as a short tutorial on C++11 futures and promises, both in answers about returning values from threads and in questions from people following along with its code.

Recommended by 3 people across 3 different months, 2011 to 2024.

What it is

Part 4 of Bartosz Milewski's C++11 concurrency video series, covering promises, futures and std::async as ways to pass results and exceptions between threads. He walks through code examples using std::thread, std::promise and std::future.

What people said

Recommends Alex B · 2011-10 · Stack Overflow · on “C++: Simple return value from std::thread?”

See this video tutorial [this video] on C++11 futures. Explicitly with threads and futures: #include <thread> #include <future> void func(std::promise<int> && p) { p.set_value(1); } std::promise<int> p; auto f = p.get_future(); std::thread t(&func, std::move(p)); t.join(); int i = f.get(); Or with std::async (higher-level wrapper for threads and futures): #include <thread> #include <future> int func() { return 1; } std::future<int> ret = std::async(&func); int i = ret.get(); I can't comment whether it works on all platforms (it seems to work on Linux, but doesn't build for me on Mac OSX with GCC 4.6.1).

Recommends user2918461 · 2017-01 · Stack Overflow · on “Using exception in g++”

I'm following this tutorial on concurrency [this video] and at 13:40 there is the line of code throw(std::exception("E")); and I get no matching function for call to ‘std::exception::exception(const char [2]). He's using VS, so maybe it does have something to do with g++?

Recommends cicada_ · 2024-05 · Stack Overflow · on “Future is not returning reference to value in C++”

I've just started learning multi-threading in C++ and following this [this video] excellent tutorial. However when I print the memory address of the string being returned in Future I'm getting a different address. Here's my code. #include<iostream> #include <thread> #include <future> #include <string> void thFunc(std::promise<std::string> && prms) { std::string str("Hello from future ^^"); std::cout << "Location=" << (void*)str.data() << "\n"; prms.set_value(str); } int main() { std::promise<std::string> prms; std::future<std::string> ftr = prms.get_future(); std::thread th(&thFunc, std::move(prms)); std::printf("Hello from main!!\n"); std::string futStr = ftr.get(); std::cout << futStr << "\t Lo

Cites Michael IV · 2015-01 · Stack Overflow · on “VC++ 2013 std::move and rvalue compile error”

I am following this tutorial [this video] on using C++ 11 threads.The following code : #include <iostream> #include <string> #include <thread> #include <future> void theFun(std::promise<std::string> && prms) { std::string str("Hellow from future"); prms.set_value(str); } int main(int argc, char* argv[]) { std::promise<std::string> prms; std::future<std::string> ftr = prms.get_future(); std::thread tr(&theFun,std::move(prms)); std::cout << "Hellow from main!" << std::endl; std::string str = ftr.get();//will block if thread still working std::cout << str << std::endl; tr.join(); } works for the tutorial author.He is using VS2010 and some experiment implementation.But in my case I am getting the fol

Cites zeo · 2017-11 · Stack Overflow · on “Sharing memory between two threads, but address of data structures changes”

@Someprogrammerdude Hey. I was following this tutorial: [this video] (You can watch it at that exact time), and for him it seems to work. I also tried copying, but the address of the string still changes no matter what I do.