Search⌘ K
AI Features

Threads vs Tasks

Explore the key differences between threads and tasks in modern C++ concurrency. Understand how threads use shared variables and join operations, while tasks leverage promises and futures for communication and synchronization. Learn how tasks handle exceptions and results differently from threads, and gain insight into blocking calls and safe communication methods.

We'll cover the following...

Threads are very different ...

C++
// 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 ...