多线程问题

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 函数。

相关推荐
以卿a1 小时前
C++ 模板初阶
开发语言·c++
黑不溜秋的7 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
Dream it possible!9 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴9 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程9 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空10 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc77411 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白112 小时前
C++——list模拟实现
开发语言·c++
程序员老舅12 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初11112 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp