code review!
C++笔记之初始化线程的所有方法
文章目录
- C++笔记之初始化线程的所有方法
- 一.非类中初始化线程
- 1.使用函数指针初始化线程
- 2.lambda表达式初始化线程
- 3.使用成员函数初始化线程
- 4.使用函数对象(Functor)初始化线程
- 5.使用std::bind绑定函数及其参数初始化线程
- 二.类中初始化线程去运行类成员函数
- 1.在构造函数中使用成员函数初始化线程
- 2.在构造函数中使用lambda表达式初始化线程
- 3.在构造函数中使用函数对象(Functor)初始化线程
- 4.使用std::bind绑定成员函数及其参数初始化线程
- 三.类中初始化线程去运行全局函数
- 四.上述程序的g++编译方法
一.非类中初始化线程
1.使用函数指针初始化线程
代码
#include <iostream>
#include <thread>
void threadFunction() {
std::cout << "Thread is running." << std::endl;
}
int main() {
std::thread t(threadFunction); // 使用函数指针初始化线程
t.join(); // 等待线程结束
return 0;
}
2.lambda表达式初始化线程
#include <iostream>
#include <thread>
void threadFunction() {
std::cout << "Thread is running." << std::endl;
}
int main() {
std::thread t([](){ std::cout << "Thread is running." << std::endl; }); // 使用lambda表达式初始化线程
t.join(); // 等待线程结束
return 0;
}
3.使用成员函数初始化线程
代码
#include <iostream>
#include <thread>
class MyClass {
public:
void threadFunction() {
std::cout << "Thread is running." << std::endl;
}
};
int main() {
MyClass obj;
std::thread t(&MyClass::threadFunction, &obj); // 使用成员函数指针初始化线程
t.join(); // 等待线程结束
return 0;
}
4.使用函数对象(Functor)初始化线程
代码
#include <iostream>
#include <thread>
class MyFunctor {
public:
void operator()() {
std::cout << "Thread is running." << std::endl;
}
};
int main() {
MyFunctor functor;
std::thread t(functor); // 使用函数对象初始化线程
t.join(); // 等待线程结束
return 0;
}
5.使用std::bind绑定函数及其参数初始化线程
代码
#include <iostream>
#include <thread>
#include <functional>
void threadFunction(int x, int y) {
std::cout << "Thread is running. Sum: " << x + y << std::endl;
}
int main() {
std::thread t(std::bind(threadFunction, 10, 20)); // 使用std::bind绑定函数及其参数初始化线程
t.join(); // 等待线程结束
return 0;
}
二.类中初始化线程去运行类成员函数
1.在构造函数中使用成员函数初始化线程
代码
#include <iostream>
#include <thread>
class MyClass {
public:
MyClass() {
thread_ = std::thread(&MyClass::threadFunction, this);
}
~MyClass() {
if (thread_.joinable()) {
thread_.join();
}
}
void threadFunction() {
std::cout << "Thread is running." << std::endl;
}
private:
std::thread thread_;
};
int main() {
MyClass obj;
// 执行其他操作...
return 0;
}
2.在构造函数中使用lambda表达式初始化线程
代码
#include <iostream>
#include <thread>
class MyClass {
public:
MyClass() {
thread_ = std::thread([this]() { threadFunction(); });
}
~MyClass() {
if (thread_.joinable()) {
thread_.join();
}
}
void threadFunction() {
std::cout << "Thread is running." << std::endl;
}
private:
std::thread thread_;
};
int main() {
MyClass obj;
// 执行其他操作...
return 0;
}
3.在构造函数中使用函数对象(Functor)初始化线程
代码
#include <iostream>
#include <thread>
class MyFunctor {
public:
void operator()() {
std::cout << "Thread is running." << std::endl;
}
};
class MyClass {
public:
MyClass() {
thread_ = std::thread(MyFunctor());
}
~MyClass() {
if (thread_.joinable()) {
thread_.join();
}
}
private:
std::thread thread_;
};
int main() {
MyClass obj;
// 执行其他操作...
return 0;
}
4.使用std::bind绑定成员函数及其参数初始化线程
代码
#include <iostream>
#include <thread>
#include <functional>
class MyClass {
public:
MyClass() {
thread_ = std::thread(std::bind(&MyClass::threadFunction, this, 10, 20));
}
~MyClass() {
if (thread_.joinable()) {
thread_.join();
}
}
void threadFunction(int x, int y) {
std::cout << "Thread is running. Sum: " << x + y << std::endl;
}
private:
std::thread thread_;
};
int main() {
MyClass obj;
// 执行其他操作...
return 0;
}