多线程问题

1.linux和windows环境下终止C++11 thread问题

目前我们创建线程大部分用的都是基于C++11中的thread,但是C++中为了各种安全问题并没有提供结束线程的方法,但是我们有时候会用到杀死C++11创建的线程,这时候就要根据我们所处的开发环境来判断了。

1.windows

在C++11中,可以使用std::thread的joinable()函数来检查线程是否可被杀死,如果线程可被杀死,可以使用std::thread的成员函数join()来等待线程执行完毕。然而,在Windows下,C++11标准库并没有提供直接的方法来杀死线程,因此我们需要使用Windows API TerminateThread来实现

cpp 复制代码
#include <iostream>
#include <thread>
#include <windows.h>

// 线程函数
void threadFunction()
{
    // 模拟一些工作
    for (int i = 0; i < 10; ++i)
    {
        std::cout << "Working..." << std::endl;
        Sleep(1000); // 线程休眠1秒
    }
}

int main()
{
    std::thread t(threadFunction); // 创建线程
    
    // 等待用户输入
    std::cout << "Press ENTER to kill the thread." << std::endl;
    std::cin.get();
    
    // 检查线程是否可被杀死
    if (t.joinable())
    {
        // 使用Windows API来终止线程
        TerminateThread(t.native_handle(), 0);
        
        // 等待线程执行完毕
        t.join();
        
        std::cout << "Thread killed." << std::endl;
    }
    
    return 0;
}

2.Linux

使用pthread_cancel

cpp 复制代码
#include <iostream>
#include <thread>
#include <chrono>
#include <pthread.h>

using namespace std;

void foo() {
	size_t x = 0;
	while (1) {
		cout << ++x << endl;
		this_thread::sleep_for(chrono::milliseconds(100));
	}
}

int main() {
	std::cerr << "1、先杀死再join\n";
	{
		thread t(foo);
		this_thread::sleep_for(chrono::seconds(1));
		cout << "t.joinable() = " << t.joinable() << endl;
		pthread_cancel(t.native_handle());
		cout << "after cancelling, t.joinable() = " << t.joinable() << endl;
		t.join();
	}
	cout << "The thread is destructed\n\n";
	this_thread::sleep_for(chrono::seconds(1));

	std::cerr << "2、先detach再杀死\n";
	{
		thread t(foo);
		pthread_t id = t.native_handle();
		t.detach();
		cout << "detached\n";
		this_thread::sleep_for(chrono::seconds(1));
		cout << "t.joinable() = " << t.joinable() << endl;
		pthread_cancel(id);
	}
	cout << "The thread is destructed\n\n";
	this_thread::sleep_for(chrono::seconds(1));

	std::cerr << "3、thread对象的析构函数不会杀死线程\n";
	{
		thread t(foo);
		t.detach();
		this_thread::sleep_for(chrono::seconds(1));
	}
	this_thread::sleep_for(chrono::seconds(1));

	return 0;
}

来源:C++11 终止一个thread对象表示的线程

tips:

在linux环境下,使用pthread_create创建线程时,如果想要将类的成员函数变成线程函数,需要将类的成员函数声明为static,而使用C++11的thread创建的线程则没有这个限制。

cpp 复制代码
void A::add(void *data)
{
    A *obj = static_cast<A *>(data);
    cout << "sum" << obj->num1 + obj->num2 << endl;
    cout << "sum::" << num1 + num2 << endl;
}
int A::num(int a, int b)
{
    num1 = a;
    num2 = b;
    pthread_t myThread;
    // int ret = pthread_create(&myThread, nullptr, A::add, this);
    std::thread t(&A::add, this, this);
    t.join();
}

注意两个this的区别:
第一个 this 表示当前对象本身的指针,它用于指定要调用的成员函数 &A::add 所在的对象。
第二个 this 是作为函数参数传递给 A::add 的参数。这表示在调用 &A::add 函数时,将当前对象的指针作为参数传递给 A::add 函数。

相关推荐
吗~喽9 分钟前
【C++】模板进阶
c语言·开发语言·c++
我不会插花弄玉19 分钟前
类与对象-下【由浅入深-C++】
c++
Tandy12356_25 分钟前
手写TCP/IP协议栈——ARP超时重新请求
c语言·c++·网络协议·计算机网络
水天需01041 分钟前
VS Code C++ 环境配置及 HelloWorld 程序
c++
初圣魔门首席弟子41 分钟前
第六章、[特殊字符] HTTP 深度进阶:报文格式 + 服务器实现(从理论到代码)
linux·网络·c++
永远都不秃头的程序员(互关)1 小时前
查找算法深入分析与实践:从线性查找到二分查找
数据结构·c++·算法
Sunsets_Red1 小时前
二项式定理
java·c++·python·算法·数学建模·c#
好评1241 小时前
C/C++ 内存管理:摆脱野指针和内存泄漏
开发语言·c++·内存管理·c/c++
威哥爱编程1 小时前
【鸿蒙开发案例篇】NAPI 实现 ArkTS 与 C++ 间的复杂对象传递
c++·harmonyos·arkts
0 0 01 小时前
CCF-CSP 37-3 模板展开(templating)【C++】
c++·算法