Thuta Learning
C++
AdvancedProgrammingbeginner

Concurrency with std::jthread

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Concurrency with std::jthread ရဲ့type, lifetime နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Warnings နဲ့sanitizers သုံးပြီးfailure cases စမ်းနိုင်ရန်
  • Modern, safe C++ code ရေးနိုင်ရန်

Concurrency correctness သည်shared mutable state, synchronization, cancellation နဲ့thread lifetime ပေါ်မူတည်ပါတယ်။ `std::jthread` ရဲ့automatic joining နဲ့stop token ကိုသုံးပြီးdata races မဖြစ်အောင်ownership သို့locking policy ကိုရှင်းပါ။

ပိုပြီးနားလည်ထားရမယ့် အချက်

Concurrency correctness သည်shared mutable state, synchronization, cancellation နဲ့thread lifetime ပေါ်မူတည်ပါတယ်။ `std::jthread` ရဲ့automatic joining နဲ့stop token ကိုသုံးပြီးdata races မဖြစ်အောင်ownership သို့locking policy ကိုရှင်းပါ။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

Concurrency with std::jthread example ကိုempty, boundary, invalid နဲ့failure cases ဖြင့်စမ်းပြီး `-std=c++23 -Wall -Wextra -Wpedantic` နဲ့build ပါ။ Lifetime-sensitive code ကိုAddressSanitizer/UndefinedBehaviorSanitizer ဖြင့်ထပ်စစ်ပါ။

ဒီသင်ခန်းစာပြီးရင်

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

ကိုယ်တိုင်စမ်းကြည့်ရန်

Concurrency with std::jthread example ကိုempty, boundary, invalid နဲ့failure cases ဖြင့်စမ်းပြီး `-std=c++23 -Wall -Wextra -Wpedantic` နဲ့build ပါ။ Lifetime-sensitive code ကိုAddressSanitizer/UndefinedBehaviorSanitizer ဖြင့်ထပ်စစ်ပါ။

Memory/Safety သတိပေးချက်

Output တစ်ကြိမ်မှန်ရုံဖြင့် lifetime, bounds, ownership နဲ့undefined behavior အားလုံးမှန်ပြီဟုယူဆခြင်း။

C++ Multithreading Referencecppreference

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Output တစ်ကြိမ်မှန်ရုံဖြင့် lifetime, bounds, ownership နဲ့undefined behavior အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Raw pointer သို့iterator တစ်ခုကိုowner container/resource ပြောင်းပြီးနောက်လည်းvalid ဖြစ်နေမယ်ဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Concurrency with std::jthread example ကိုempty, boundary, invalid နဲ့failure cases ဖြင့်စမ်းပြီး `-std=c++23 -Wall -Wextra -Wpedantic` နဲ့build ပါ။ Lifetime-sensitive code ကိုAddressSanitizer/UndefinedBehaviorSanitizer ဖြင့်ထပ်စစ်ပါ။

You'll know it worked when: task complete

Concurrency with std::jthread | Thuta Learning