C++ 在类里面使用多线程技术

有很多时候,我们希望可以在C++类里面对那些比较耗时的函数使用多线程技术,但是,C++类的成员函数的函数指针不能直接做为参数传到pthread_create,主要因为是C++成员函数指针带有类命名空间,同时成员函数末尾是会被C++编译器加上可以接收对象地址的this指针参数。因此需要将成员函数做一定的转化,将其转化为不被编译器加上this指针,而由我们自己来为该函数维护”this”指针即可。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class Test
{
    public:
        int sum;
        int cnt;
    Test(){sum=0;}
    public:
        int insert();
        void * insert_pth(void*);
        void lanch();
};
int Test::insert()
{
    sleep(2);
    sum+=1;
}
void * Test::insert_pth(void*)
{
    insert();
}
void Test::lanch()
{
    pthread_t pth;
    pthread_create(&pth,NULL,insert_pth,NULL);
}
int main()
{
    Test t;
    t.lanch();
    return 0;
}

只需将insert_pth变化为static函数,同时将insert逻辑代码转移到insert_pth即可:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class Test
{
    public:
        int sum;
        int cnt;
    Test(){sum=0;}
    public:
        int insert();
    static  void * insert_pth(void*);
        void lanch();
};
int Test::insert()
{
    sleep(2);
    sum+=1;
    printf("%d insert.....\n",sum);
}
void * Test::insert_pth(void* __this)
{
    Test * _this =(Test *)__this;
    sleep(2);
    _this->sum+=1;
    printf("%d insert.....\n",_this->sum);
}
void Test::lanch()
{
    pthread_t pth;
    pthread_create(&pth,NULL,insert_pth,(void*)this);
}
int main()
{
    Test t;
    t.sum=0;
    t.lanch();
    sleep(5);
    return 0;
}

注意:

在 XXX_pth()函数内容尽量不要调用类的其它成员函数,否则成员函数将无法获取正确的this指针而操作错误内存,从而导致segmantation fault

总结:

  • 将线程函数声明为静态函数;
  • 创建线程时传递进去this指针;
  • 在线程函数中使用传进来的this指针调用类的成员,特别注意调用类的成员函数时要避免segmantation fault错误。
posted @   youngliu91  阅读(3302)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
点击右上角即可分享
微信分享提示