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

  1. C++ thread类多线程编程

    https://blog.csdn.net/dcrmg/article/details/53912941 多线程操作的thread类,简单多线程示例: #include <iostream> ...

  2. C++使用thread类多线程编程

    转自:C++使用thread类多线程编程 C++11中引入了一个用于多线程操作的thread类,下面进行简单演示如何使用,以及如果进行多线程同步. thread简单示例 #include <io ...

  3. C++使用thread类进行多线程编程

    C++11中引入了一个用于多线程操作的thread类,简单多线程示例: #include <iostream> #include <thread> #include <W ...

  4. java ee Concurrency 并发编程

    https://www.javacodegeeks.com/2014/07/java-ee-concurrency-api-tutorial.html This is a sample chapter ...

  5. PatentTips - Sleep state mechanism for virtual multithreading

    BACKGROUND The present disclosure relates generally to information processing systems and, more spec ...

  6. C++使用Windows API CreateMutex函数多线程编程

    C++中也可以使用Windows 系统中对应的API函数进行多线程编程.使用CreateThread函数创建线程,并且可以通过CreateMutex创建一个互斥量实现线程间数据的同步: #includ ...

  7. c++的并发操作(多线程)

    C++11标准在标准库中为多线程提供了组件,这意味着使用C++编写与平台无关的多线程程序成为可能,而C++程序的可移植性也得到了有力的保证.另外,并发编程可提高应用的性能,这对对性能锱铢必较的C++程 ...

  8. 【Java并发系列01】Thread及ThreadGroup杂谈

    img { border: solid black 1px } 一.前言 最近开始学习Java并发编程,把学习过程记录下.估计不是那么系统,主要应该是Java API的介绍(不涉及最基础的概念介绍), ...

  9. Java多线程01(Thread类、线程创建、线程池)

    Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...

  10. 多线程的实现方式01 Thread

    /* * 多线程 有三种实现方式 * 其一 Thread * * 写一个类 * * 1.让他继承 Thread * 2.重写thread中的run方法 * 3.创建子类对象就是在 创建线程! * 3. ...

随机推荐

  1. Linux系统安装JDK8

    一.卸载现用的JDK 1.查看Linux自带的JDK是否已安装 查看是否安装openjdk,java  -version (yum安装的 一般都是 OpenJDK     命令:yum install ...

  2. Java入门系列之重写

    前言 关于所有Java系列文章面向有一定基础的童鞋,所写每一篇希望有一定含金量,有些内容可能会从Java整个语法全局考虑穿插后续要讲解的内容以成系统,若不理解,请看完后再学习.上一节我们讲解完了fin ...

  3. ArcGIS 10.2安装及卸载教程

    卸载 在控制面板中找到程序->卸载程序 找到ArcGIS的相关软件 这里以ArcGIS 10.2 Destop为例,选中,然后点击卸载,会出现如下界面 选择Remove,然后根据提示进行操作,即 ...

  4. TX 1核4G2M云服务器,376/2年,可免费续1年

    腾讯云个人开发者活动 https://cloud.tencent.com/act/developer

  5. Redis的字符串底层是啥?为了速度和安全做了啥?

    面试场景 面试官:Redis有哪些数据类型? 我:String,List,set,zset,hash 面试官:没了? 我:哦哦哦,还有HyperLogLog,bitMap,GeoHash,BloomF ...

  6. ES6标准中的import和export

    在ES6前, 前端使用RequireJS或者seaJS实现模块化, requireJS是基于AMD规范的模块化库,  而像seaJS是基于CMD规范的模块化库,  两者都是为了为了推广前端模块化的工具 ...

  7. 设计模式:state模式

    核心: 把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化 例子: class State //状态接口 { public: virtual void show() = 0; ...

  8. 设计模式:abstract factory模式

    含义:抽象工厂将“抽象零件”组装成“抽象产品” 理解:相比于工厂方法模式,可以根据不同的接口创建不同的产品,说白了就是将一个接口变成两个接口,各自返回不同的抽象产品 例子: class Car //抽 ...

  9. git 缓存密码 unable to access... 403错误

    如果输入了 git config credential.helper 命令之后还是出现了osxkeychain, store 或者 cache 等,说明 git 的配置还是没有被清空,我参考了stac ...

  10. linux中neovim+tmux安装与配置遇到的问题

    Neovim 安装与配置 安装 pip3 install neovim 之前安装过anaconda,默认安装python3和pip3 检查状态 :checkhealth 终端输入'nvim' 进入nv ...