多线程开发要点1:如何使用类的成员函数创建线程

本文介绍两种在C++中创建线程的方法:一是通过pthread_create函数实现,二是利用C++11标准库中的std::thread类。文中提供了具体的代码实例,展示了如何在类的成员函数中启动线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何使用类的成员函数创建线程(c++11 thread和pthread_create)?

创建线程方案1: pthread_create的方式

NAME
       pthread_create - create a new thread

SYNOPSIS
       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

       Compile and link with -pthread.

DESCRIPTION
       The pthread_create() function starts a new thread in the calling process.  The new thread starts execution by invoking start_routine(); arg is
       passed as the sole argument of start_routine().
pthreadTest.cpp

#include "pthreadTest.h"                                                                                                                                 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;
pthreadTest::pthreadTest()
{
}


pthreadTest::~pthreadTest()
{
}


void pthreadTest::_threadSon() {
    cout << "This is son thread" << endl;
}

void pthreadTest::createSonThread(){
    pthread_t son_thread_id;
    pthread_create(&son_thread_id, NULL, _thread_t<pthreadTest, &pthreadTest::_threadSon>, (void *)(new pthreadTestTemp(this)));
    int d = pthread_detach(son_thread_id);
}

int main() {
    pthreadTest* pthreadTestp = new pthreadTest();
    pthreadTestp->createSonThread();
    cout << "This is main thread" << endl;
    return 0;
}
pthreadTest.h

#pragma once

#include <stdlib.h>
class pthreadTest
{
public:
    pthreadTest();
    ~pthreadTest();
    void _threadSon();
    void createSonThread();
};


class pthreadTestTemp {
public:
    pthreadTestTemp(pthreadTest* p) {
        this->p1 = p;
    }
    pthreadTest* p1;
};

//引入模板类,目的是让线程函数满足void *(*start_routine) (void *)的形式
template <typename TYPE, void (TYPE::*_tsBLControl)()>
void* _thread_t(void* param)
{
    pthreadTestTemp* mpthreadTestTempp = (pthreadTestTemp*)param;
    ((TYPE*)(mpthreadTestTempp->p1))->_threadSon();
    return NULL;
}

/*翻译过来:
template <typename TYPE, void (TYPE::*_tsBLControl)()>
void* _thread_t(void* param)
{
    pthreadTestTemp* mpthreadTestTempp = (pthreadTestTemp*)param;
    ((pthreadTest*)(mpthreadTestTempp->p1))->_threadSon();
    return NULL;
}
这样,就可以调用pthreadTest类的成员函数_threadSon()了.
*/

注:编译方式

$ g++ -o pthreadTest pthreadTest.cpp -pthread

创建线程方案2:使用C++11里的thread

thread_test.h

#pragma once                                                                                                                                             
#include <stdio.h>  
#include <stdlib.h>  
#include <iostream> // std::cout  
#include <thread>   // std::thread 
using namespace std;
class threadTestTemp {
public:
    threadTestTemp():switchTest(true),numberTest(256){

    }   
    ~threadTestTemp(){}
    bool switchTest;
    int numberTest;
};

class threadTest
{
public:
    threadTest();
    ~threadTest();
    void _threadSon(threadTestTemp* set, int date);
    void createSonThread(threadTestTemp* set);
};
thread_test.cpp


#include "threadTest.h"                                                                                                                                  

using namespace std;
threadTest::threadTest(){
}
threadTest::~threadTest(){
}

void threadTest::_threadSon(threadTestTemp* set, int date) {
    cout << "This is son thread" << "set->switchTest is" << set->switchTest << endl;
    cout << "This is son thread" << "set->numberTest is" << set->numberTest << endl;
    set->switchTest = false;
    set->numberTest = 128;
    cout << "This is son thread" << "set->switchTest is" << set->switchTest << endl;
    cout << "This is son thread" << "set->numberTest is" << set->numberTest << endl;
    cout << "Date is " << date << endl;
}

void threadTest::createSonThread(threadTestTemp* set){
    int time = 2017;
    thread t;
    t = thread(std::mem_fn(&threadTest::_threadSon), this, std::ref(set), time);//有多少参数想要往新建的线程函数里传,就尽情的放吧,O(∩_∩)O~
    t.join();
}

int main() {
    threadTest* threadTestp = new threadTest();
    threadTestTemp* threadTestTempp =  new threadTestTemp();
    threadTestp->createSonThread(threadTestTempp);
    cout << "This is main thread" << endl;
    cout << "This is main thread" << "set->switchTest is" << threadTestTempp->switchTest << endl;
    cout << "This is main thread" << "set->numberTest is" << threadTestTempp->numberTest << endl;
    return 0;
}
注:编译方式
$ g++ -o threadTest threadTest.cpp -std=c++11 -pthread
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值