当前位置: 首页 > news >正文

c++线程thread示例

本文章记录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!
http://www.lryc.cn/news/290347.html

相关文章:

  • Compose | UI组件(十一) | Spacer - 留白
  • PyTorch的nn.Module类的详细介绍
  • python使用activemq库ActiveMQClient类的连接activemq并订阅、发送和接收消息
  • 【Flutter 面试题】Dart是什么?Dart和Flutter有什么关系?
  • 前后台分离跨域交互
  • React16源码: React中处理LegacyContext相关的源码实现
  • Boost.Test资源及示例
  • 数据结构二叉树
  • JavaScript继承与原型链
  • SouthLeetCode-打卡24年01月第4周
  • Linux——磁盘和文件系统(一)
  • EasyCVR视频智能监管系统方案设计与应用
  • Ubuntu搭建国标平台wvp-GB28181-pro
  • LC 2808. 使循环数组所有元素相等的最少秒数
  • Qt|大小端数据转换
  • 禅道添加自定义字段
  • 蓝桥杯2024/1/26笔记-----基于PCF8591的电压采集装置
  • 【一】esp32芯片开发板环境搭建
  • PyTorch2ONNX-分类模型:速度比较(固定维度、动态维度)、精度比较
  • Docker命令快车道:一票通往高效开发之旅
  • IP类接口大全,含免费次数
  • LLMs 的记忆和信息检索服务器 Motorhead
  • vue3项目中让echarts适应div的大小变化,跟随div的大小改变图表大小
  • springboot启动异常
  • 直播主播之互动率与促单
  • Android 基础技术——Bitmap
  • 数据结构奇妙旅程之七大排序
  • 【JavaScript】Generator
  • 河南省考后天网上确认,请提前准备证件照哦
  • 【前端】防抖和节流