非同期シーケンス図を mermaid を使って描くよ!

ソースコード

#include <iostream>
#include <thread>
#include <chrono>

class A {
public:
    void longRunningTask() {
        std::cout << "30秒間かかる処理" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(30));
        std::cout << "30秒間かかる処理 終了" << std::endl;
    }

    void shortTask() {
        std::cout << "shortTask 処理完了" << std::endl;
    }
};

int main() {
    std::cout << "main thread started" << std::endl;
    A objA1;

    // スレッドでFunctionAを非同期実行
    std::thread t(&A::longRunningTask, &objA1);

    // メインスレッドで同期実行
    objA1.shortTask();
    
    { // 使ったらすぐ破棄
        A objA2;
        objA2.shortTask();
    }

    std::cout << "スレッドの終了を待つ" << std::endl;
    t.join();

    std::cout << "main thread finished" << std::endl;
    return 0;
}

mermaid

sequenceDiagram
participant main as main thread

note over main: main thread started

create participant objA1 as A
main ->> objA1: new
create participant thread as other thread

main -) thread: new
thread ->>+ objA1: longRunningTask
note over objA1: 30秒間かかる処理

main ->>+ objA1: shortTask
objA1 -->>- main: return

create participant objA2 as A
main ->> objA2: new
main ->>+ objA2: shortTask
objA2 -->>- main: return
destroy objA2
main -x objA2: delete

note over main: スレッドの終了を待つ
main ->>+ thread: join

note over objA1: 30秒間かかる処理 終了
objA1 -->>- thread: return
thread -->>- main: スレッド終了

note over main: main thread finished