c++线程thread示例

本文详细介绍了如何在C++中使用`std::thread`和`std::mutex`创建、启动和结束线程,以及处理线程同步的方法。通过实例展示了如何在`ThreadTest`类中实现线程入口点和控制线程生命周期的操作。

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

本文章记录c++创建线程,启动线程和结束线程的代码。

需要注意,编译时需要添加-lpthread依赖。

代码:

ThreadTest.h

#ifndef TEST_THREAD_TEST_H
#define TEST_THREAD_TEST_H

#include <thread>
#include <mutex>

class ThreadTest
{

public:
    
    void start();
    void stop();
    void threadLoop(int a);
    volatile bool started = false;

private:
    std::thread *mThread;
    std::mutex mMutex;
};

static void threadRun(ThreadTest* threadTest);

#endif // TEST_THREAD_TEST_H

ThreadTest.cpp

#include "ThreadTest.h"
#include "iostream"

// thread entrance.
static void threadRun(ThreadTest* threadTest){
    printf("thread start!\n");
    int a = 0;
    while (threadTest->started)
    {
        a++;
        threadTest->threadLoop(a);
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    printf("threadRun method exit!\n");
};

// start thread.
void ThreadTest::start(){
    mMutex.lock();
    if(started){
        mMutex.unlock();
        return;
    }
    started = true;
    printf("thread starting!\n");
    mThread = new std::thread(threadRun, this);
    printf("thread started!\n");
    mMutex.unlock();
};

// stop thread.
void ThreadTest::stop(){
    mMutex.lock();
    if(!started) {
        mMutex.unlock();
        return;
    }
    if(started && mThread != nullptr && mThread->joinable()) {
        started = false;
        mThread->join();
    }
    printf("thread stopped!\n");
    mMutex.unlock();
};

// run in thread.
void ThreadTest::threadLoop(int a){
    printf("threadLoop, a:%d!\n", a);
};

Test.cpp

#include "ThreadTest.h"
#include "iostream"

// thread entrance.
static void threadRun(ThreadTest* threadTest){
    printf("thread method called!\n");
    int a = 0;
    while (threadTest->started)
    {
        a++;
        threadTest->threadLoop(a);
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    printf("threadRun method exit!\n");
};

// start thread.
void ThreadTest::start(){
    mMutex.lock();
    if(started){
        mMutex.unlock();
        return;
    }
    started = true;
    printf("thread starting!\n");
    mThread = new std::thread(threadRun, this);
    printf("thread started!\n");
    mMutex.unlock();
};

// stop thread.
void ThreadTest::stop(){
    mMutex.lock();
    if(!started) {
        mMutex.unlock();
        return;
    }
    if(started && mThread != nullptr && mThread->joinable()) {
        started = false;
        mThread->join();
    }
    printf("thread stopped!\n");
    mMutex.unlock();
};

// run in thread.
void ThreadTest::threadLoop(int a){
    printf("threadLoop, a:%d!\n", a);
};

执行:

导入IDE执行,或用g++:
g++ -o test Test.cpp -I ThreadTest.h ThreadTest.cpp -lpthread
./test

输出

hello world!
thread starting!
thread started!
thread method called!
threadLoop, a:1!
threadLoop, a:2!
threadLoop, a:3!
threadRun method exit!
thread stopped!
-----------------
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadLoop, a:2!
threadLoop, a:3!
threadRun method exit!
thread stopped!
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadRun method exit!
thread stopped!
thread starting!
thread started!
thread method called!
threadRun method exit!
thread stopped!
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadRun method exit!
thread stopped!
thread starting!
thread started!
thread method called!
threadRun method exit!
thread stopped!
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadRun method exit!
thread stopped!
hello world end!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值