随笔 - 156  文章 - 0  评论 - 1  阅读 - 12万

随笔分类 -  C/C++多线程

线程学习九: 条件变量
摘要:条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。 1. 锁与条件变量之初始化 静态初始化 pthread_mutex_t mutex 阅读全文
posted @ 2022-05-12 17:11 JJ_S 阅读(169) 评论(0) 推荐(0) 编辑
线程学习八:C++11 线程对象创建后既不join()也不detach()的后果
摘要:C++11中,线程对象(std::thread)创建后,有两种状态: joinable nonjoinable 线程对象通过默认构造函数构造后状态为nonjoinable;线程对象通过有参构造函数创建后状态为joinable。joinable状态的线程对象被调用join()或者detach()会变成 阅读全文
posted @ 2022-03-04 15:36 JJ_S 阅读(545) 评论(0) 推荐(0) 编辑
线程学习七:几个例子
摘要:#1. 主线程中创建2个子线程,子线程按顺序执行 && 主线程比子线程结束晚 // 在子线程中通过join()方法指定顺序 #include <iostream> #include <thread> #include <chrono> // C++里处理多线程的头文件是thread using na 阅读全文
posted @ 2022-03-04 15:22 JJ_S 阅读(62) 评论(0) 推荐(0) 编辑
线程学习六:pthread使用barrier栅栏方式同步
摘要:Linux中提供了多种同步机制,其中使用barrier(栅栏)是多线程之间进行同步的方法之一。 基本原理: 假设多个线程约定一个栅栏,只有当所有的线程都达到这个栅栏时,栅栏才会放行,否则到达此处的线程将被阻塞。 使用场景: 程序启动的时候,需要建立一个独立的线程去做一些特殊的工作。比如这个线程需要初 阅读全文
posted @ 2022-03-04 14:29 JJ_S 阅读(1207) 评论(0) 推荐(0) 编辑
线程学习五:线程加锁(2)--pthread
摘要:pthread加锁 参考: https://blog.csdn.net/xiaolong1126626497/article/details/122362586 阅读全文
posted @ 2022-03-04 14:13 JJ_S 阅读(59) 评论(0) 推荐(0) 编辑
线程学习五:线程加锁(1)
摘要:#1.锁:mutex(互斥量) 锁,是生活中应用十分广泛的一种工具。锁的本质属性是为事物提供“访问保护”,例如:大门上的锁,是为了保护房子免于不速之客的到访; 自行车的锁,是为了保护自行车只有owner才可以使用;保险柜上的锁,是为了保护里面的合同和金钱等重要东西…… 在c++等高级编程语言中,锁也 阅读全文
posted @ 2022-03-03 17:49 JJ_S 阅读(302) 评论(0) 推荐(0) 编辑
线程学习四:连接和分离线程
摘要:1.std::thread方法 在声明一个std::thread对象之后,都可以使用detach和join函数来启动被调线程,区别在于两者是否阻塞主调线程。 (1)当使用join()函数时,主调线程阻塞,等待被调线程终止,然后主调线程回收被调线程资源,并继续运行; (2)当使用detach()函数时 阅读全文
posted @ 2022-03-03 17:42 JJ_S 阅读(570) 评论(0) 推荐(0) 编辑
线程学习三:创建线程
摘要:1. 线程创建 1.1 std::thread创建线程 用普通函数创建线程 用成员函数创建线程 用类对象创建线程 用Lambda表达式创建线程 1.1.1 用普通函数创建线程 #include <iostream> #include <thread> void foo(int a) { std::c 阅读全文
posted @ 2022-03-03 13:59 JJ_S 阅读(133) 评论(0) 推荐(0) 编辑
线程学习二:std::thread与pthread对比
摘要:1. std::thread与pthread对比 std::thread是C++11接口,使用时需要包含头文件#include <thread>,编译时需要支持c++11标准。thread中封装了pthread的方法,所以也需要链接pthread库 pthread是C++98接口且只支持Linux, 阅读全文
posted @ 2022-03-03 11:08 JJ_S 阅读(9225) 评论(0) 推荐(1) 编辑
线程学习一:多线程与多进程
摘要:在一台计算机中,我们可以同时打开许多软件,比如同时浏览网页、听音乐、打字等等,看似非常正常。但仔细想想,为什么计算机可以做到这么多软件同时运行呢?这就涉及到计算机中的两个重要概念:多进程和多线程了。 1.多进程 进程是程序在计算机上的一次执行活动,即正在运行中的应用程序,通常称为进程。当你运行一个程 阅读全文
posted @ 2022-03-02 19:49 JJ_S 阅读(232) 评论(0) 推荐(0) 编辑
C++11多线程 – Part 8:std::future,std::promise以及线程的返回值
摘要:翻译自:https://thispointer.com//c11-multithreading-part-8-stdfuture-stdpromise-and-returning-values-from-thread/ stdfuture对象可以与async,stdpackaged_task和std 阅读全文
posted @ 2020-06-01 11:11 JJ_S 阅读(640) 评论(0) 推荐(0) 编辑
C++11 多线程 - Part 7:条件变量
摘要:翻译自:https://thispointer.com//c11 multithreading part 7 condition variables explained/ 在本文中,我们将通过示例讨论C++ 11多线程中条件变量的用法。 条件变量 条件变量是一种事件,用于在两个或多个线程之间进行信号 阅读全文
posted @ 2020-04-22 15:29 JJ_S 阅读(414) 评论(0) 推荐(0) 编辑
C++11 多线程 - Part 6:事件处理的需要
摘要:翻译自:https://thispointer.com//c11 multithreading part 6 need of event handling/ 在本文中,我们将讨论多线程中事件处理的需要。 有时一个线程需要等待一个事件发生,比如一个条件变为真,或者一个任务由另一个线程完成。 例如,假设 阅读全文
posted @ 2020-04-22 11:59 JJ_S 阅读(384) 评论(0) 推荐(0) 编辑
C++11 多线程 - Part 5:使用锁解决争用条件
摘要:翻译自:https://thispointer.com//c11 multithreading part 5 using mutex to fix race conditions/ 在本文中,我们将讨论如何在多线程环境中使用互斥锁来保护共享数据,并避免争用条件。 为了修复多线程环境中的争用条件,我们 阅读全文
posted @ 2020-04-20 16:50 JJ_S 阅读(308) 评论(0) 推荐(0) 编辑
C++11 多线程 - Part 4: 数据共享和争用条件
摘要:翻译自:https://thispointer.com//c11 multithreading part 4 data sharing and race conditions/ 在多线程环境中,线程间的数据共享非常容易。但是这种简单的数据共享可能会造成应用程序出现问题。其中一个问题就是争用条件。 什 阅读全文
posted @ 2020-04-20 12:03 JJ_S 阅读(274) 评论(0) 推荐(0) 编辑
c++11多线程(转)
摘要:转:https://blog.csdn.net/ouyangfushu/article/details/80199140 [toc] 1. 普通函数多线程调用 1.1 无参数函数 c include include using namespace std; class TestThread { pu 阅读全文
posted @ 2020-04-15 18:39 JJ_S 阅读(169) 评论(0) 推荐(0) 编辑
C++11多线程 - Part 3: 为线程传递参数
摘要:翻译:https://thispointer.com//c11 multithreading part 3 carefully pass arguments to threads/ 为了给一个线程相关联的调用对象或函数传递参数,仅仅只需要在std::thread构造时传递额外的参数。 默认情况下,所 阅读全文
posted @ 2020-04-15 17:46 JJ_S 阅读(847) 评论(0) 推荐(0) 编辑
C++11多线程 - Part 2: 连接和分离线程
摘要:翻译:https://thispointer.com//c11 multithreading part 2 joining and detaching threads/ 本篇讨论的是std::thread的连接和分离 使用std::thread::join()连接线程 一旦一个线程启动,那么另一个线 阅读全文
posted @ 2020-04-15 16:14 JJ_S 阅读(1291) 评论(0) 推荐(0) 编辑
C++11 多线程- Part 1 : 创建线程的三种方式
摘要:翻译自:https://thispointer.com//c 11 multithreading part 1 three different ways to create threads/ 这篇文章主要讨论在C++11中如何利用std::thread创建线程 C++11线程库介绍 最早的C++标准 阅读全文
posted @ 2020-04-14 14:03 JJ_S 阅读(949) 评论(0) 推荐(0) 编辑

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示