当前位置: 首页 > 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!

相关文章:

  • qt5-入门
  • UE4 CustomDepthMobile流程小记
  • 业务逐字稿
  • 在探最大模定理
  • TR-069/TR-369设备管理
  • vue3 el-pagination 将组件中英文‘goto’ 修改 为 中文到‘第几’
  • Google Chrome 中出现 ERR_SSL_KEY_USAGE_INCOMPATIBLE 错误
  • yolov5 deepsort 行人/车辆(检测 +计数+跟踪+测距+测速)
  • 在数字化营销的大背景下,企业微信营销的挑战与应对策略
  • 【C++】wxWidgets编程的程序入口点
  • 桥接模式解析
  • 【vue】vue.config.js里面获取本机ip:
  • QML语法 | 导入语句(import)
  • WPF自定义控件,聚合器模式传递消息
  • 面试题整理
  • __proto__ 和 prototype的关系
  • 5、React组件事件详解
  • Angular Elements 及其运作原理
  • canvas 五子棋游戏
  • CentOS 7 防火墙操作
  • django开发-定时任务的使用
  • ES2017异步函数现已正式可用
  • ES6 ...操作符
  • sessionStorage和localStorage
  • SQL 难点解决:记录的引用
  • Web标准制定过程
  • 聊聊directory traversal attack
  • 聊聊sentinel的DegradeSlot
  • 码农张的Bug人生 - 见面之礼
  • 配置 PM2 实现代码自动发布
  • 前端设计模式
  • 设计模式(12)迭代器模式(讲解+应用)
  • 通过git安装npm私有模块
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 一天一个设计模式之JS实现——适配器模式
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • ionic异常记录
  • ​MySQL主从复制一致性检测
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • # Apache SeaTunnel 究竟是什么?
  • #每天一道面试题# 什么是MySQL的回表查询
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (9)目标检测_SSD的原理
  • (a /b)*c的值
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Python第六天)文件处理
  • (分布式缓存)Redis分片集群
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (算法二)滑动窗口
  • (转)母版页和相对路径