c++线程thread示例

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

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

代码:

ThreadTest.h

cpp 复制代码
#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

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

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);
};

执行:

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

输出

cpp 复制代码
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!
相关推荐
coding随想1 小时前
JavaScript中的BOM:Window对象全解析
开发语言·javascript·ecmascript
念九_ysl1 小时前
Java 使用 OpenHTMLToPDF + Batik 将含 SVG 遮罩的 HTML 转为 PDF 的完整实践
java·开发语言·pdf
yaoxin5211231 小时前
124. Java 泛型 - 有界类型参数
java·开发语言
liulilittle2 小时前
深度剖析:OPENPPP2 libtcpip 实现原理与架构设计
开发语言·网络·c++·tcp/ip·智能路由器·tcp·通信
88号技师2 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
勤奋的知更鸟2 小时前
Java 编程之模板方法模式
java·开发语言·模板方法模式
十年编程老舅3 小时前
跨越十年的C++演进:C++20新特性全解析
c++·c++11·c++20·c++14·c++23·c++17·c++新特性
上单带刀不带妹3 小时前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架
-凌凌漆-4 小时前
【Qt】QStringLiteral 介绍
开发语言·qt