// 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使用的更多相关文章

  1. 以Android环境为例的多线程学习笔记(二)-----------------锁和条件机制

    现在的绝大多数应用程序都是多线程的程序,而当有两个或两个以上的线程需要对同一数据进行存取时,就会出现条件竞争,也即 是这几个线程中都会有一段修改该数据状态的代码.但是如果这些线程的运行顺序推行不当的话 ...

  2. c++11多线程学习笔记之四 生产消费者

    #ifndef MY_QUEUE_H__ #define MY_QUEUE_H__ #include<list> #include<mutex> #include<thr ...

  3. c++11多线程学习笔记之三 condition_variable使用

    从windows角度来说,condition_variable类似event. 阻塞等待出发,不过condition_variable可以批量出发. 代码如下: // 1111111.cpp : 定义 ...

  4. c++11多线程学习笔记之一 thread基础使用

    没啥好讲的  c++11  thread类的基本使用 #include "stdafx.h" #include <iostream> #include <thre ...

  5. Java多线程学习笔记(二)

    三 多线程执行的共享数据和非共享数据: 共享数据:就是每个线程执行的时候共享数据使用,比如这个线程一个为5的数据,减少为4之后,另一个线程执行拿到的数据是4,两个线程执行的数据是共享的. 非共享数据: ...

  6. java 多线程学习笔记(二) -- IO密集型任务

    IO密集型是指对IO操作较多的任务.下面以查询一些股票价格任务为例: YahooFinance.java public class YahooFinance { public static doubl ...

  7. Java多线程学习笔记(二)——Executor,Executors,ExecutorService比较

    Executor:是Java线程池的超级接口:提供一个execute(Runnable command)方法;我们一般用它的继承接口ExecutorService. Executors:是java.u ...

  8. Java多线程学习笔记之二缓存

    1.高速缓存 由来:处理器处理能力原因大于主内存(DRAM)访问速率,为了弥补这个差距,引入了高速缓存. 高速缓存是一种存取速率远比主内存大而容量远比主内存小的存储部件,每一个处理器都有其高速缓存.在 ...

  9. Java多线程学习笔记(一)——多线程实现和安全问题

    1. 线程.进程.多线程: 进程是正在执行的程序,线程是进程中的代码执行,多线程就是在一个进程中有多个线程同时执行不同的任务,就像QQ,既可以开视频,又可以同时打字聊天. 2.线程的特点: 1.运行任 ...

随机推荐

  1. zabbix修改中文乱码

    参考网站; https://blog.csdn.net/open_data/article/details/47447029 字体下载网站: http://www.font5.com.cn/zitix ...

  2. re(正则)模块

    import re # re 存在5种使用方式 #1. macth #2.search #3.findall #4.split #5 sub re.match('^chen', 'chenhua123 ...

  3. autolayout UILabel 设置最大宽度

    label1.preferredMaxLayoutWidth = 100: label1.numberOfLines = 0; //自适应行数

  4. 关于gevent的一些理解(二)

    3 实际应用 1 zeromq和gevent: zeromq的介绍请参看:http://www.infoq.com/cn/news/2010/09/introduction-zero-mq 假设你已经 ...

  5. Mongodb 折腾笔记

    简介: Mongodb 是一个由 C++ 语言编写的基于分布式文件存储的数据库,是目前最像关系型数据库的非关系型数据库. 下载地址:https://fastdl.mongodb.org/linux/m ...

  6. unity 返回子对象组件

    Component[] GetComponentsInChildren(Type t, bool includeInactive = false); //includeInactive: 是否查找非激 ...

  7. delphi实现映射和断开网络驱动器

    type TNetDiskMapper=class private FNetResource: TNetResource; FUserName,FPassWord:PWideChar; public ...

  8. ASP.NETAutocomplete control

    分享一个Ajax autocomplete control, 原文链接:http://forums.asp.net/t/1157595.aspx 首先,引入ScriptManager <asp: ...

  9. 吴裕雄 实战python编程(2)

    from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...

  10. 进化的Spark, 从DataFrame说起

    进化的Spark, 从DataFrame说起:http://www.tuicool.com/articles/IzeY7zM