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!
相关推荐
别来无恙1491 分钟前
岛屿周长问题的三种解法:直接计数法、数学计算法与深度优先搜索
java·c++·算法·深度优先·dfs
liujing102329291 小时前
Day13_C语言基础&项目实战
c语言·开发语言
周振超的1 小时前
c++编译第三方项目报错# pragma warning( disable: 4273)
开发语言·c++
JH30732 小时前
Java Stream API 在企业开发中的实战心得:高效、优雅的数据处理
java·开发语言·oracle
愚润求学4 小时前
【递归、搜索与回溯】FloodFill算法(一)
c++·算法·leetcode
呆呆的小草5 小时前
Cesium距离测量、角度测量、面积测量
开发语言·前端·javascript
uyeonashi5 小时前
【QT系统相关】QT文件
开发语言·c++·qt·学习
冬天vs不冷6 小时前
Java分层开发必知:PO、BO、DTO、VO、POJO概念详解
java·开发语言
sunny-ll6 小时前
【C++】详解vector二维数组的全部操作(超细图例解析!!!)
c语言·开发语言·c++·算法·面试
猎人everest7 小时前
Django的HelloWorld程序
开发语言·python·django