0
0

スレッド処理

Last updated at Posted at 2024-09-25

スレッド処理

th1とth2のスレッドを作成し、実行する。
コンパイルの時に-pthreadを付けて実行すること。

#include <stdio.h>
#include <iostream>
#include <thread>

void ThreadFunc(int num) {
  std::cout << "ThreadFunc " << num << std::endl;
}

int main() {
  std::thread th1(ThreadFunc, 1);
  
  auto func = [](std::string msg) {
    std::cout << "thread message : " << msg << std::endl;
  };

  std::thread th2(func, "Hello, World!");
  std::cout << "th2 id : " << th2.get_id() << std::endl;

  th1.join();
  th2.join();

  return 0;
}

実行結果

th2 id : 140081530832448
thread message : Hellow, World!
ThreadFunc 1

備忘録

-pthreadでコメント来ていたので、備忘録
昔のことを忘れず、発生した時のために対応するメモ書き
現場によっては環境が古いなんてざらにある

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0