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. ...
随机推荐
- Java工具类之:包装类
Java工具类--包装类 我们都知道,JDK 其实给我们提供了很多很多 Java 开发者已经写好的现成的类,他们其实都可以理解成工具类,比如我们常见的集合类,日期相关的类,数学相关的类等等,有了这些工 ...
- day11 本日作业+周末作业
目录 一.今日作业 1.编写文件copy工具 2.编写登录程序,账号密码来自于文件 3.编写注册程序,账号密码来存入文件 二.周末综合作业: 1.编写用户登录接口 2.编写程序实现用户注册后,可以登录 ...
- CSS(二)- 属性速览(含版本、继承性和简介)
相关链接 CSS3速查表,这里面列出了所有新增的属性以及新增或者修改的属性值 css参考手册,很好地一个常用网站 CSS定位(不可继承) CSS布局(仅visibility可继承) CSS尺寸(不可继 ...
- [网鼎杯 2020 青龙组]AreUSerialz
题目分析 <?php include("flag.php"); highlight_file(FILE); class FileHandler { protected $op ...
- scrapy shell 的使用
是什么?:是一个终端下的调试工具,用来调试scrapy 安装ipython :pip install ipython 启动: scrapy shell + 需要请求的url 进来之后,response ...
- EF实现简单的增删改查
1.在项目中添加ADO.NET实体数据模型: 2.接着根据提示配置数据库连接,配置完毕之后项目中生成了大致如下的内容(EF6.x): 其中TestData.tt中的Consumer,Stores是创建 ...
- bzoj4300绝世好题
bzoj4300绝世好题 题意: 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0.n≤100000,ai≤10^9. 题解: 用f[i]表示当前二进制i为1 ...
- 区分C语言中的指针函数和函数指针
1.指针函数: 类型说明符 *函数名(形参表) { .......... /*函数体*/ .......... /*函数体*/ } 其中函数名之前加了"*"号表明,这是一 ...
- Ethical Hacking - NETWORK PENETRATION TESTING(12)
Post Connection Attacks Sophisticated attacks that can be used after connecting to the target AP. Ga ...
- 不藏了,摊牌了,一张知识图谱整理完整Java并发体系,就问全不全
推荐阅读: 2020年马士兵Java多线程高并发讲解——百万年薪架构师告诉你Java多线程与高并发 目录 这是我关于整个Java并发体系的整理,结合的主要是现在市面上对于Java并发在面试的过程中经常 ...