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!
相关推荐
晨曦5432109 分钟前
图(Graph):关系网络的数学抽象
开发语言·算法·php
Ustinian_3101 小时前
【C/C++】For 循环展开与性能优化【附代码讲解】
c语言·开发语言·c++
牵牛老人1 小时前
Qt 插件开发全解析:从接口定义,插件封装,插件调用到插件间的通信
开发语言·qt
钮钴禄·爱因斯晨1 小时前
AIGC浪潮下,风靡全球的Mcp到底是什么?一文讲懂,技术小白都知道!!
开发语言·人工智能·深度学习·神经网络·生成对抗网络·aigc
22jimmy2 小时前
JavaWeb(二)CSS
java·开发语言·前端·css·入门·基础
机器视觉知识推荐、就业指导4 小时前
面试问题详解五:Qt 信号与槽的动态管理
开发语言·qt
MZ_ZXD0016 小时前
springboot汽车租赁服务管理系统-计算机毕业设计源码58196
java·c++·spring boot·python·django·flask·php
岁忧8 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油8 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
四维碎片10 小时前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio