thread-01
- // 8:15 AM/09/28/2017
- #pragma once
- #include <iostream> // std::cout
- #include <thread> // std::thread
- #include <mutex> // std::mutex
- #include <chrono>
- using namespace std;
- volatile int counter();
- volatile int counter2();
- mutex mtx;//This mutex class is synchronization primitive that
- //can be used to protect shared data from being simultaneously accessed by multiple threads.
- // mutex类是一个同步原语,用来保护共享数据,阻止多线程同时访问
- mutex mtx2;
- void run()
- {
- for (int i = ; i < ; ++i)
- {
- mtx.lock();// lock mtx,blocks if mtx is not available
- // the word block means that when the mtx is avaiable,it will lock mtx and the following code will being executed
- ++counter;
- cout << this_thread::get_id() << "==> " << counter << endl;
- mtx.unlock();// this function will make mtx is available,
- //and other threads that is being blocked will detect the mtx is available
- // but the others don't mean that all of them can detect the mtx is available because if one detect it and it will lock it.
- // only the one thread will own the mtx
- //here the function unlock is necessary
//一般不直接使用mutex 而用 std::unique_lock, std::lock_guard等
//mutex is usually not accessed directly- }
- }
- void run2()
- {
- for (int i = ; i < ; i++)
- {
- if (mtx2.try_lock())
- //It differs from the function lock.Here,it will not block and if mtx2 is available,it will be lock and return ture.
- {
- ++counter2;
- cout << this_thread::get_id() << "==> " << counter2 << endl;
- mtx2.unlock();
- }
- }
- }
- int main(int argc, const char* argv[])
- {
- thread ts[];
- for (int i = ; i < ; ++i)
- {
- ts[i] = thread(run);
- }
- for (auto& t : ts) t.join();
- std::this_thread::sleep_for(std::chrono::seconds());
- // sleep for 2s
- thread ts2[];
- for (int i = ; i < ; ++i)
- {
- ts2[i] = thread(run2);
- }
- for (auto& t : ts2)t.join();
- }
- //We see that the results of counter and counter2 are not same,and we convincingly konw the counter is equal
- //to 1000 because of the function lock.The counter2,however,may not have a unique result owing to the function
- // try_lock without blocking.
thread-01的更多相关文章
- C++ thread类多线程编程
https://blog.csdn.net/dcrmg/article/details/53912941 多线程操作的thread类,简单多线程示例: #include <iostream> ...
- C++使用thread类多线程编程
转自:C++使用thread类多线程编程 C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步. thread简单示例 #include <io ...
- C++使用thread类进行多线程编程
C++11中引入了一个用于多线程操作的thread类,简单多线程示例: #include <iostream> #include <thread> #include <W ...
- java ee Concurrency 并发编程
https://www.javacodegeeks.com/2014/07/java-ee-concurrency-api-tutorial.html This is a sample chapter ...
- PatentTips - Sleep state mechanism for virtual multithreading
BACKGROUND The present disclosure relates generally to information processing systems and, more spec ...
- C++使用Windows API CreateMutex函数多线程编程
C++中也可以使用Windows 系统中对应的API函数进行多线程编程.使用CreateThread函数创建线程,并且可以通过CreateMutex创建一个互斥量实现线程间数据的同步: #includ ...
- c++的并发操作(多线程)
C++11标准在标准库中为多线程提供了组件,这意味着使用C++编写与平台无关的多线程程序成为可能,而C++程序的可移植性也得到了有力的保证.另外,并发编程可提高应用的性能,这对对性能锱铢必较的C++程 ...
- 【Java并发系列01】Thread及ThreadGroup杂谈
img { border: solid black 1px } 一.前言 最近开始学习Java并发编程,把学习过程记录下.估计不是那么系统,主要应该是Java API的介绍(不涉及最基础的概念介绍), ...
- Java多线程01(Thread类、线程创建、线程池)
Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...
- 多线程的实现方式01 Thread
/* * 多线程 有三种实现方式 * 其一 Thread * * 写一个类 * * 1.让他继承 Thread * 2.重写thread中的run方法 * 3.创建子类对象就是在 创建线程! * 3. ...
随机推荐
- Socket模拟Web服务器
效果如下: 源码下载地址:https://github.com/doyoulaikeme/DotNetSample/tree/master/DotNetSample3/SocketWebServer
- day81 初识drf
目录 一.web应用模式 二.API接口 1 rpc(远程过程调用/远程服务调用) 2 restful(资源状态转换) 三.RESTful API规范 四.序列化 五.Django Rest_Fram ...
- Linux 下载工具推荐: Motrix && qbittorrent
Linux下载介绍 Linux下其实下载工具还是蛮多的, 命令行的wget,curl,aria2,甚至于apt 但是个人日常使用下还是有图形化界面比较方便易用.大多数教程里推荐的Uget,可能是我也不 ...
- python PEP8开发规范
为了使得代码更美观,方便阅读,建议遵循下PEP8规范 每行长度最大不要超过79. 换行可以使用反斜杠,换行点要在操作符的后面敲回车. 类个top-level函数定义之间空两行:类中的方法定义之间空一行 ...
- 数据可视化之 图表篇(五) PowerBI图表不够炫酷?来看看这个
现在这个大数据时代,每时每刻.各行各业都在产生多种多样的海量数据,如何简单高效的来理解.挖掘这些数据,发现背后的见解就非常重要. 本文介绍这个图表就可以帮你快速发现海量数据背后的见解,微软研究院打造的 ...
- 机器学习实战基础(十七):sklearn中的数据预处理和特征工程(十)特征选择 之 Embedded嵌入法
Embedded嵌入法 嵌入法是一种让算法自己决定使用哪些特征的方法,即特征选择和算法训练同时进行.在使用嵌入法时,我们先使用某些机器学习的算法和模型进行训练,得到各个特征的权值系数,根据权值系数从大 ...
- (四)学习了解OrchardCore笔记——将模块的名字添加到程序集的ModuleName
关于如何将模块名添加到程序集的ModuleName说简单吧也简单,说不简单吧也不简单. 简单的原因是代码只有几行,不简单的原因是这些都不是c#,都是MSbuild的代码.这可真难为我了,所以这个地方我 ...
- java大数据最全课程学习笔记(3)--HDFS 简介及操作
目前CSDN,博客园,简书同步发表中,更多精彩欢迎访问我的gitee pages 目录 HDFS 简介及操作 HDFS概述 HDFS产出背景及定义 HDFS优缺点 HDFS组成架构 HDFS文件块大小 ...
- 小书MybatisPlus第5篇-Active Record模式精讲
本文为一个系列中的第五节,前四节访问如下地址: 小书MybatisPlus第1篇-整合SpringBoot快速开始增删改查 小书MybatisPlus第2篇-条件构造器的应用及总结 小书Mybatis ...
- 题解 UVA1193 Radar Installation
原题 PDF OJ 思路 分析 因为半径d已经确定,所以对于每个点,我们可以算出它在x 轴上的覆盖位置线段LR,如图. 此问题便转为: 对于 n 个区间,每个区间内至少有1个点,求最少点数. 算法 我 ...