Thuta Learning
C++
AdvancedProgrammingbeginner

Concurrency with std::jthread

What you'll walk away with

  • Explain the type, lifetime, and runtime behavior of Concurrency with std::jthread
  • Use warnings and sanitizers to test failure cases
  • Write modern and safer C++

Concurrency correctness depends on shared mutable state, synchronization, cancellation, and thread lifetime. Use std::jthread automatic joining and stop tokens, and define ownership or locking so data races cannot occur.

Build a Complete Mental Model

Concurrency correctness depends on shared mutable state, synchronization, cancellation, and thread lifetime. Use std::jthread automatic joining and stop tokens, and define ownership or locking so data races cannot occur.

Apply It in Modern C++

Build an original Concurrency with std::jthread example with `-std=c++23 -Wall -Wextra -Wpedantic` and test empty, boundary, invalid, and failure cases. Run lifetime-sensitive code under AddressSanitizer and UndefinedBehaviorSanitizer.

After This Lesson

cpp
#include <iostream>
#include <stop_token>
#include <thread>

int main() {
    std::jthread worker([](std::stop_token) { std::cout << "task complete\n"; });
}
You should see
task complete

Try It Yourself

Build an original Concurrency with std::jthread example with `-std=c++23 -Wall -Wextra -Wpedantic` and test empty, boundary, invalid, and failure cases. Run lifetime-sensitive code under AddressSanitizer and UndefinedBehaviorSanitizer.

Memory and Safety Warning

Assuming one correct output proves lifetime, bounds, ownership, and undefined behavior are all correct.

C++ Multithreading Referencecppreference

Easy traps

  • Assuming one correct output proves lifetime, bounds, ownership, and undefined behavior are all correct.
  • Assuming a raw pointer or iterator remains valid after its owning container or resource changes.

Hands-on Exercise

Build an original Concurrency with std::jthread example with `-std=c++23 -Wall -Wextra -Wpedantic` and test empty, boundary, invalid, and failure cases. Run lifetime-sensitive code under AddressSanitizer and UndefinedBehaviorSanitizer.

You'll know it worked when: task complete

Concurrency with std::jthread | Thuta Learning