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

相关文章:

c++线程thread示例

本文章记录c创建线程&#xff0c;启动线程和结束线程的代码。 需要注意&#xff0c;编译时需要添加-lpthread依赖。 代码&#xff1a; ThreadTest.h #ifndef TEST_THREAD_TEST_H #define TEST_THREAD_TEST_H#include <thread> #include <mutex>class ThreadTes…...

Compose | UI组件(十一) | Spacer - 留白

文章目录 前言Spacer组件的参数说明Spacer组件的使用 总结 前言 Spacer组件是让两组件之间留有空白间隔 Spacer组件的参数说明 Spacer只有一个修饰符&#xff0c;修饰留空白的大小和比例&#xff0c;颜色 Spacer(modifier: Modifier)Spacer组件的使用 Row {Box(modifier M…...

PyTorch的nn.Module类的详细介绍

在PyTorch中&#xff0c;nn.Module 类是构建神经网络模型的基础类&#xff0c;所有自定义的层、模块或整个神经网络架构都需要继承自这个类。nn.Module 类提供了一系列属性和方法用于管理网络的结构和训练过程中的计算。 1. PyTorch中nn.Module基类的定义 在PyTorch中&#xff…...

python使用activemq库ActiveMQClient类的连接activemq并订阅、发送和接收消息

引入activemq模块&#xff1a;from activemq import ActiveMQClient from activemq import ActiveMQClient 是一个Python的导入语句&#xff0c;它从activemq模块中导入了ActiveMQClient类。 解释一下各个部分&#xff1a; from activemq: 这表示我们正在从一个名为activemq…...

【Flutter 面试题】Dart是什么?Dart和Flutter有什么关系?

【Flutter 面试题】Dart是什么&#xff1f;Dart和Flutter有什么关系&#xff1f; 文章目录 写在前面Dart是什么Dart和Flutter有什么关系&#xff1f; 写在前面 &#x1f44f;&#x1f3fb; 正在学 Flutter 的同学&#xff0c;你好&#xff01; &#x1f60a; 本专栏是解决 Fl…...

前后台分离跨域交互

后台处理跨域 安装插件 >: pip install django-cors-headers插件参考地址&#xff1a;https://github.com/ottoyiu/django-cors-headers/项目配置&#xff1a;dev.py # 注册app INSTALLED_APPS [...corsheaders, ]# 添加中间件 MIDDLEWARE [...corsheaders.middleware.…...

React16源码: React中处理LegacyContext相关的源码实现

LegacyContext 老的 contextAPI 也就是我们使用 childContextTypes 这种声明方式来从父节点为它的子树提供 context 内容的这么一种方式遗留的contextAPI 在 react 17 被彻底移除了&#xff0c;就无法使用了那么为什么要彻底移除这个contextAPI的使用方式呢&#xff1f;因为它…...

Boost.Test资源及示例

Note&#xff1a;boost_1_84_0的动态连接库资源链接 1.代码组织如下图&#xff1a; 2.包括程序入口的代码文件 示例&#xff1a; // M24.01.MyTestModule.cpp : 定义控制台应用程序的入口点。 //#include "stdafx.h" #define BOOST_TEST_MODULE MYTESTMODULE #def…...

数据结构二叉树

二叉树是数据结构中的一个基本概念&#xff0c;它是每个节点最多有两个子节点的树结构。在二叉树中&#xff0c;每个节点通常有两个指针&#xff0c;分别指向左子节点和右子节点。 数据结构定义 在二叉树的节点中&#xff0c;通常包含以下信息&#xff1a; 数据域&#xff1…...

JavaScript继承与原型链

继承和原型链是什么&#xff1f; 1.1 在继承中&#xff0c;子类继承父类的特征和行为&#xff0c;使得子类对象具有父类的实例域和方法。这意味着子类可以使用父类的方法和属性&#xff0c;使用继承的目的是为了更好设置实例的公共属性和方法&#xff0c;如下例子&#xff1a; …...

SouthLeetCode-打卡24年01月第4周

SouthLeetCode-打卡24年01月第4周 // Date : 2024/01/22 ~ 2024/01/28 022.设计链表 - 双链表 (1) 题目描述 022#LeetCode.707.#北岸计划2024/01/22 (2) 题解代码 import java.util.List;class ListNode {int val;ListNode prev;ListNode next;ListNode(){this.val 0;th…...

Linux——磁盘和文件系统(一)

Linux——磁盘和文件系统 磁盘机械式磁盘固态硬盘 机械式磁盘结构磁盘&#xff0c;磁道&#xff0c;扇区柱面 文件系统的初始化划卷&#xff08;划盘&#xff09; 挂载C盘放了什么东西Boot Block&#xff08;启动模块&#xff09; 0号组放了什么东西Super Block&#xff08;超级…...

EasyCVR视频智能监管系统方案设计与应用

随着科技的发展&#xff0c;视频监控平台在各个领域的应用越来越广泛。然而&#xff0c;当前的视频监控平台仍存在一些问题&#xff0c;如视频质量不高、监控范围有限、智能化程度不够等。这些问题不仅影响了监控效果&#xff0c;也制约了视频监控平台的发展。 为了解决这些问…...

Ubuntu搭建国标平台wvp-GB28181-pro

目录 简介安装和编译1.查看操作系统信息2.安装最新版的nodejs3.安装java环境4.安装mysql5.安装redis6.安装编译器7.安装cmake8.安装依赖库9.编译ZLMediaKit9.1.编译结果说明 10.编译wvp-GB28181-pro10.1.编译结果说明 配置1.WVP-PRO配置文件1.1.Mysql数据库配置1.2.REDIS数据库…...

LC 2808. 使循环数组所有元素相等的最少秒数

2808. 使循环数组所有元素相等的最少秒数 难度: 中等 题目大意&#xff1a; 给你一个下标从 0 开始长度为 n 的数组 nums 。 每一秒&#xff0c;你可以对数组执行以下操作&#xff1a; 对于范围在 [0, n - 1] 内的每一个下标 i &#xff0c;将 nums[i] 替换成 nums[i] &…...

Qt|大小端数据转换

后面打算写Qt关于网络编程的博客&#xff0c;网络编程就绕不开字节流数据传输&#xff0c;字节流数据的传输一般是根据协议来定义对应的报文该如何组包&#xff0c;那这就必然牵扯到了大端字节序和小端字节序的问题了。不清楚的大小端的可以看一下相关资料&#xff1a;大小端模…...

禅道添加自定义字段

1&#xff0c;数据库表 zt_story 添加自定义字段 bakDate1&#xff0c;bakDate2&#xff0c;bakDate3&#xff0c;bakDate4 2&#xff0c;在 /opt/lampp/htdocs/zentaopms/extension/custom/story/ext/config 中添加bakDate.php文件 <?php $config->story->datatab…...

蓝桥杯2024/1/26笔记-----基于PCF8591的电压采集装置

功能实现要求&#xff1a; 每次建好工程文件夹&#xff0c;里边包含User&#xff08;放工程文件&#xff0c;mian.c&#xff0c;可以在这里写如同我这个文章的文本文档&#xff09;、Driver&#xff08;存放底层文件如Led.c&#xff0c;Led.h等&#xff09; 新建的工程先搭建框…...

【一】esp32芯片开发板环境搭建

1、esp32的源码在github上的地址 不同的芯片支持的源码版本不一样&#xff0c;需要根据自己的实际的esp32开发板的芯片下载不用版本的代码 esp32支持多种开发方式&#xff0c;如arduino&#xff0c;ESP-IDF等。官方推荐使用idf开发&#xff0c;ESP-IDF 是乐鑫官方推出的物联网开…...

PyTorch2ONNX-分类模型:速度比较(固定维度、动态维度)、精度比较

图像分类模型部署: PyTorch -> ONNX 1. 模型部署介绍 1.1 人工智能开发部署全流程 #mermaid-svg-bAJun9u4XeSykIbg {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-bAJun9u4XeSykIbg .error-icon{fill:#552222;}…...

简易版抽奖活动的设计技术方案

1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...

MFC内存泄露

1、泄露代码示例 void X::SetApplicationBtn() {CMFCRibbonApplicationButton* pBtn GetApplicationButton();// 获取 Ribbon Bar 指针// 创建自定义按钮CCustomRibbonAppButton* pCustomButton new CCustomRibbonAppButton();pCustomButton->SetImage(IDB_BITMAP_Jdp26)…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力

引言&#xff1a; 在人工智能快速发展的浪潮中&#xff0c;快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型&#xff08;LLM&#xff09;。该模型代表着该领域的重大突破&#xff0c;通过独特方式融合思考与非思考…...

Unit 1 深度强化学习简介

Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库&#xff0c;例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体&#xff0c;比如 SnowballFight、Huggy the Do…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

【Oracle】分区表

个人主页&#xff1a;Guiat 归属专栏&#xff1a;Oracle 文章目录 1. 分区表基础概述1.1 分区表的概念与优势1.2 分区类型概览1.3 分区表的工作原理 2. 范围分区 (RANGE Partitioning)2.1 基础范围分区2.1.1 按日期范围分区2.1.2 按数值范围分区 2.2 间隔分区 (INTERVAL Partit…...

MySQL 8.0 事务全面讲解

以下是一个结合两次回答的 MySQL 8.0 事务全面讲解&#xff0c;涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容&#xff0c;并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念&#xff08;ACID&#xff09; 事务是…...

免费数学几何作图web平台

光锐软件免费数学工具&#xff0c;maths,数学制图&#xff0c;数学作图&#xff0c;几何作图&#xff0c;几何&#xff0c;AR开发,AR教育,增强现实,软件公司,XR,MR,VR,虚拟仿真,虚拟现实,混合现实,教育科技产品,职业模拟培训,高保真VR场景,结构互动课件,元宇宙http://xaglare.c…...

Web后端基础(基础知识)

BS架构&#xff1a;Browser/Server&#xff0c;浏览器/服务器架构模式。客户端只需要浏览器&#xff0c;应用程序的逻辑和数据都存储在服务端。 优点&#xff1a;维护方便缺点&#xff1a;体验一般 CS架构&#xff1a;Client/Server&#xff0c;客户端/服务器架构模式。需要单独…...