c++11多线程学习笔记之二 mutex使用
// 1111111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex> int gcounter = 0;
std::mutex gmtx;
std::mutex gmtxOutput; void Increases() {
for (int i = 0; i<10000; ++i) {
if (gmtx.try_lock()) { // only increase if currently not locked:
++gcounter;
gmtx.unlock();
}
else{
gmtxOutput.lock();
std::cout << "try lock failed" << std::endl;
gmtxOutput.unlock();
}
}
} int _tmain(int argc, _TCHAR* argv[]) {
std::thread threads[10];
for (int i = 0; i<10; ++i)
threads[i] = std::thread(Increases); for (auto& th : threads)
th.join();
std::cout << "counter is " << gcounter << std::endl; return 0;
}
输出:
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
try lock failed
counter is 99983
请按任意键继续. . .
这个例子说明了 try_lock() 与 lock()的区别
try_lock()会对能否上锁进行测试 并返回布尔值
而lock()则直接进行锁定 不能锁定则阻塞直到锁定
c++11多线程学习笔记之二 mutex使用的更多相关文章
- 以Android环境为例的多线程学习笔记(二)-----------------锁和条件机制
现在的绝大多数应用程序都是多线程的程序,而当有两个或两个以上的线程需要对同一数据进行存取时,就会出现条件竞争,也即 是这几个线程中都会有一段修改该数据状态的代码.但是如果这些线程的运行顺序推行不当的话 ...
- c++11多线程学习笔记之四 生产消费者
#ifndef MY_QUEUE_H__ #define MY_QUEUE_H__ #include<list> #include<mutex> #include<thr ...
- c++11多线程学习笔记之三 condition_variable使用
从windows角度来说,condition_variable类似event. 阻塞等待出发,不过condition_variable可以批量出发. 代码如下: // 1111111.cpp : 定义 ...
- c++11多线程学习笔记之一 thread基础使用
没啥好讲的 c++11 thread类的基本使用 #include "stdafx.h" #include <iostream> #include <thre ...
- Java多线程学习笔记(二)
三 多线程执行的共享数据和非共享数据: 共享数据:就是每个线程执行的时候共享数据使用,比如这个线程一个为5的数据,减少为4之后,另一个线程执行拿到的数据是4,两个线程执行的数据是共享的. 非共享数据: ...
- java 多线程学习笔记(二) -- IO密集型任务
IO密集型是指对IO操作较多的任务.下面以查询一些股票价格任务为例: YahooFinance.java public class YahooFinance { public static doubl ...
- Java多线程学习笔记(二)——Executor,Executors,ExecutorService比较
Executor:是Java线程池的超级接口:提供一个execute(Runnable command)方法;我们一般用它的继承接口ExecutorService. Executors:是java.u ...
- Java多线程学习笔记之二缓存
1.高速缓存 由来:处理器处理能力原因大于主内存(DRAM)访问速率,为了弥补这个差距,引入了高速缓存. 高速缓存是一种存取速率远比主内存大而容量远比主内存小的存储部件,每一个处理器都有其高速缓存.在 ...
- Java多线程学习笔记(一)——多线程实现和安全问题
1. 线程.进程.多线程: 进程是正在执行的程序,线程是进程中的代码执行,多线程就是在一个进程中有多个线程同时执行不同的任务,就像QQ,既可以开视频,又可以同时打字聊天. 2.线程的特点: 1.运行任 ...
随机推荐
- Tornado之链接数据库
5 数据库 知识点 torndb安装 连接初始化 执行语句 execute execute_rowcount 查询语句 get query 5.1 数据库 与Django框架相比,Tornado没有自 ...
- MYSQL体系结构-来自期刊
MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...
- OpenACC 梯度下降法求解线性方程的优化
▶ 书上第二章,用一系列步骤优化梯度下降法解线性方程组.才发现 PGI community 编译器不支持 Windows 下的 C++ 编译(有 pgCC 命令但是不支持 .cpp 文件,要专业版才支 ...
- windows10系统右键添加cmd命令
https://blog.csdn.net/Mr_BEelzebub/article/details/78776104 首先,在桌面新建一个文本文档. Windows Registry Editor ...
- 36. CentOS-6.3安装Mysql集群
安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...
- 表析LESS、Sass和Stylus的异同
. 首页 博客园 联系我 前言:CSS预处理语言. 基本差别. 基本语法. 变量与作用域. 混合(Mixins). 嵌套实现后代选择器. 继承. 条件语句. 循环语句. 综合对比. 留言评论 返回顶部 ...
- vue基础——条件渲染
一.v-if 1.1.v-if,v-else 在字符串模板中,比如Handlebars,我们得像这样写一个条件块: HTML <!-- Handlebars 模板 --> {{#if ok ...
- 【转载】Apache Jena TDB CRUD operations
Apache Jena TDB CRUD operations June 11, 2015 by maltesander http://tutorial-academy.com/apache-jena ...
- VirtualBox“切换到无缝模式”和“自动调整显示尺寸”菜单无法使用
现象:VirtualBox“切换到无缝模式”和“自动调整显示尺寸”菜单无法使能,无法全窗口显示虚拟机桌面,菜单状态如下图所示: 原因:该功能的支持需要安装VirtualBox增强功能 方法:安装Vir ...
- 【337】Text Mining Using Twitter Streaming API and Python
Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...