1、简单使用
一个h一个cpp文件
#pragma once
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <string>
class Person {
public:
struct dog {
std::string name;
int age;
};
public:
void a(std::atomic<bool>& running, int param1, double param2, const std::string& param3);
void startA(int param1, double param2, const std::string& param3);
};
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <string>
#include "osg.h"
void Person::a(std::atomic<bool>& running, int param1, double param2, const std::string& param3) {
std::cout << "Function a started with params " << param1 << ", " << param2 << ", " << param3 << "." << std::endl;
// 模拟耗时操作
while (running) {
// 执行一些操作
}
std::cout << "Function a ended." << std::endl;
}
void Person::startA(int param1, double param2, const std::string& param3) {
std::atomic<bool> running(true);
// 创建并启动线程
std::thread thread_a(&Person::a, this, std::ref(running), param1, param2, param3);
std::this_thread::sleep_for(std::chrono::seconds(10));
if (running.load()) {
std::cout << "stop" << std::endl;
running = false;
}
// 等待线程完成执行
if (thread_a.joinable()) {
thread_a.join();
}
}
int main() {
Person person;
person.startA(42, 3.14, "Hello");
// ... 其他代码 ...
return 0;
}
2、方法A中存在引用
错误示例:
将sum执行的数据保存下来
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <string>
#include "osg.h"
void Person::a(std::atomic<bool>& running, int& sum, double param2) {
std::cout << "Function a started with params " << sum << ", " << param2 << std::endl;
// 模拟耗时操作
while (running) {
// 执行一些操作
for (int i = 0; i < 10000000; i++) {
sum = i;
}
}
std::cout << "Function a ended." << std::endl;
}
void Person::startA(int param1, double param2, const std::string& param3) {
for (int i = 0; i < 2; i++) {
int sum = 0;
std::atomic<bool> running(true);
// 创建并启动线程
std::thread thread_a(&Person::a, this, std::ref(running), sum, param2);
std::this_thread::sleep_for(std::chrono::seconds(5));
if (running.load()) {
std::cout << "stop" << std::endl;
running = false;
}
// 等待线程完成执行
if (thread_a.joinable()) {
thread_a.join();
std::cout << "等待线程完成执行" << std::endl;
}
std::cout << "sum: " <<sum<< std::endl;
}
std::cout << "任务结束" << std::endl;
}
int main() {
Person person;
person.startA(42, 3.14, "Hello");
// ... 其他代码 ...
return 0;
}
编译都过不去
正确示例:
只修改一句
std::thread thread_a(&Person::a, this, std::ref(running), std::ref(sum), param2);
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <string>
#include "osg.h"
void Person::a(std::atomic<bool>& running, int& sum, double param2) {
std::cout << "Function a started with params " << sum << ", " << param2 << std::endl;
// 模拟耗时操作
while (running) {
// 执行一些操作
for (int i = 0; i < 1000000000; i++) {
sum = i;
}
}
std::cout << "Function a ended." << std::endl;
}
void Person::startA(int param1, double param2, const std::string& param3) {
for (int i = 0; i < 2; i++) {
int sum = 0;
std::atomic<bool> running(true);
// 创建并启动线程
std::thread thread_a(&Person::a, this, std::ref(running), std::ref(sum), param2);
std::this_thread::sleep_for(std::chrono::seconds(5));
if (running.load()) {
std::cout << "stop" << std::endl;
running = false;
}
// 等待线程完成执行
if (thread_a.joinable()) {
thread_a.join();
std::cout << "等待线程完成执行" << std::endl;
}
std::cout << "sum: " <<sum<< std::endl;
}
std::cout << "任务结束" << std::endl;
}
int main() {
Person person;
person.startA(42, 3.14, "Hello");
// ... 其他代码 ...
return 0;
}
3、A方法阻塞时间过长,希望停止
如果Person::a 方法卡住了,那么 if (thread_a.joinable()) { thread_a.join(); } 就阻塞了,std::cout << "sum: " <<sum<< std::endl; 值就得不到了,除非a方法完成了
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <string>
#include "osg.h"
void Person::a(std::atomic<bool>& running, int& sum, const double ¶m2) {
std::cout << "Function a started with params " << sum << ", " << param2 << std::endl;
// 模拟耗时操作
for (int i = 0; i < 10; i++) {
std::this_thread::sleep_for(std::chrono::seconds(1));
sum = i;
}
std::cout << "Function a ended." << std::endl;
}
void Person::startA(int param1, double param2, const std::string& param3) {
for (int i = 0; i < 2; i++) {
int sum = 0;
std::atomic<bool> running(true);
// 创建并启动线程
std::thread thread_a(&Person::a, this, std::ref(running), std::ref(sum), param2);
std::this_thread::sleep_for(std::chrono::seconds(3));
if (running.load()) {
std::cout << "stop" << std::endl;
running = false;
}
// 等待线程完成执行
if (thread_a.joinable()) {
thread_a.join();
}
std::cout << "sum: " <<sum<< std::endl;
}
std::cout << "任务结束" << std::endl;
}
int main() {
Person person;
person.startA(42, 3.14, "Hello");
// ... 其他代码 ...
return 0;
}
修改:
在每次循环、处理小任务、遍历某数据 中 进行running判断
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <string>
#include "osg.h"
void Person::a(std::atomic<bool>& running, int& sum, const double ¶m2) {
std::cout << "Function a started with params " << sum << ", " << param2 << std::endl;
// 模拟耗时操作
for (int i = 0; i < 10; i++) {
if (!running) break;
std::this_thread::sleep_for(std::chrono::seconds(1));
sum = i;
}
std::cout << "Function a ended." << std::endl;
}
void Person::startA(int param1, double param2, const std::string& param3) {
for (int i = 0; i < 2; i++) {
int sum = 0;
std::atomic<bool> running(true);
// 创建并启动线程
std::thread thread_a(&Person::a, this, std::ref(running), std::ref(sum), param2);
std::this_thread::sleep_for(std::chrono::seconds(3));
if (running.load()) {
std::cout << "stop" << std::endl;
running = false;
}
// 等待线程完成执行
if (thread_a.joinable()) {
thread_a.join();
}
std::cout << "sum: " <<sum<< std::endl;
}
std::cout << "任务结束" << std::endl;
}
int main() {
Person person;
person.startA(42, 3.14, "Hello");
// ... 其他代码 ...
return 0;
}