C++11通过函数指针创建线程
程序员文章站
2022-07-05 18:11:27
...
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<thread>
using namespace std;
void func() {
cout << "my thread::func(),thread id is:" << this_thread::get_id() << endl;
}
void func1(string s) {
cout << "my thread::func1(),the arg is " << s << " thread id is:" << this_thread::get_id() << endl;
}
void func2(string &s) {
cout << "&s:" << &s << endl;
cout << "my thread::func2(),the arg is " << s << " ,thread id is:" << this_thread::get_id() << endl;
}
void func3(int a, int b) {
cout << "my thread::func3()" << a + b << endl;
}
int main(){
thread th = thread(func);
th.join();
cout << "main thread::func(),thread id is:" << this_thread::get_id() << endl;
cout << endl;
thread th1 = thread(func1, "渣渣猫");
th1.join();
cout << endl;
string str = "土拨鼠";
thread th2 = thread(func2, std::ref(str));
th2.join();
cout << "&s:" << &str << endl;
cout << "main thread::func2(),the arg is " << str << " ,thread id is:" << this_thread::get_id() << endl;
cout << endl;
thread th3 = thread(func3, 5, 6);
th3.join();
system("pause");
return 0;
}
推荐阅读
-
创建函数/类的线程
-
linux 线程创建 pthread_create函数 获取线程id的方法
-
C++11通过函数指针创建线程
-
python 如何用map()函数创建多线程任务
-
C/C++编程教训----函数内静态类对象初始化非线程安全(C++11之前)
-
VC++中多线程学习(MFC多线程)一(线程的创建、线程函数如何调用类成员呢?如何调用主对话框的成员?、MFC中的工作线程和界面线程的区别)
-
创建函数/类的线程
-
linux 线程创建 pthread_create函数 获取线程id的方法
-
java多线程之:通过 Callable 和 Future 创建线程
-
并发系列一:初始java线程与os的关系,模拟java调用os函数创建线程