Threads vs. Tasks

This lesson highlights the differences between the threads and tasks that are used in C++ for concurrency.

We'll cover the following...

Threads are very different from tasks. Let’s look at this piece of code to see how:

Press + to interact
// asyncVersusThread.cpp
#include <future>
#include <thread>
#include <iostream>
int main(){
std::cout << std::endl;
int res;
std::thread t([&]{ res = 2000 + 11; });
t.join();
std::cout << "res: " << res << std::endl;
auto fut= std::async([]{ return 2000 + 11; });
std::cout << "fut.get(): " << fut.get() << std::endl;
std::cout << std::endl;
}

The child thread t and the asynchronous function call ...