thread 学习

#include <thread>
#include <cstdio>
#include <utility>
#include <iostream>

void print(int x) {
    printf("%d\n", x);
}

int main() {
    std::thread t1(print, 1);
    std::thread t2(print, 2);

    // 获得线程ID
    std::thread::id t1_id = t1.get_id();
    std::thread::id t2_id = t2.get_id();
    std::cout << t1_id << " " << t2_id << std::endl;    
    
    /* 
     * 阻塞当前线程,直至 *this 所标识的线程完成其执行。
     * *this 所标识的线程的完成同步于从 join() 的成功返回。
     */
    t1.join();
    t2.join();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wuwangchuxin0924/p/10230851.html