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. ...
随机推荐
- 测试必备工具之抓包神器 Charles 如何抓取 https 数据包?
之前发过一篇文章讲解了Charles抓包工具的基本使用(有需要的小伙伴可以去看上一篇文章), 讲的数据包主要是http协议,大家可以看到数据包并直接显示具体详细的内容: 但是如果抓到的是https的 ...
- 循序渐进VUE+Element 前端应用开发(15)--- 用户管理模块的处理
在前面随笔介绍了ABP+Vue前后端的整合处理,包括介绍了ABP的后端设计,以及前端对ABP接口API的ES6的封装,通过JS的继承类处理,极大减少了重复臃肿的代码,可以简化对后端API接口的封装,而 ...
- oracle 12c数据库在Windows环境下的安装
因为菜鸟小白之前做着一些数据库审计产品的测试,接下来我会分享一些关于数据库安装和通过python的访问数据库的知识 安装 首先我们需要下载一个oracle 12c的安装程序,解压后右键点击“ ...
- 如何手写一个ArrayList
写完HashMap,觉得手痒痒,所以隔了一天再来实现一下简单的ArrayList,ArrayList相比而言就非常的简单,主要的核心点有以下几个方面: 1.ArrayList的底层是由数组构成的 2. ...
- iconfont - 好用免费的图标库
某里出品 打开首页???????搜索框在哪里 网站:点我
- [开源硬件DIY] 自制一款精致炫酷的蓝牙土壤温湿度传感器,用于做盆栽呵护类产品(API开放,开发者可自行DIY微信小程序\安卓IOS应用)
目录 前言: 1. 成品展示 2. 原理图解析 3. pcb设计 4. 嵌入式对外提供接口 4.1 蓝牙广播 4.2 蓝牙服务和属性 4.3 数据包格式 4.4 数据通信模型 重要 . 前言: 本期给 ...
- idea 自动生成try/catch代码块的快捷键
好像每个人的快捷键可能不同:我的是 Alt+Shift+Z 网上查的是 Ctrl+Alt+T 如果都不是可以点选工具栏生成try/catch(并可查看到自己的快捷键是什么):Code->Su ...
- 记IntelliJ IDEA创建spring mvc一次坑爹的操作!!!!
本人刚开始学习spring mvc,遇到一问题,现在分享一下. 点击Next,创建项目完成,你会发现缺少很多东西. lib文件没有,里面的jar更没有.applicationContext.xml和d ...
- vue学习(十六) 自定义私有过滤器 ES6字符串新方法 填充字符串
<div id="app"> <p>{{data | formatStr('yyyy-MM-dd')}}</p></div> //s ...
- HRNet + Object Contextual Representation
文章内容来自CCF-CV走进高校报告会中MSRA王井东老师的报告"Learning high-resolution and object-contextual representations ...