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. ...
随机推荐
- 常用js代码片段(一)
1.如果数组所有元素都满足函数条件,则返回true.调用时,如果省略第二个参数,则默认传递布尔值. const all= (arr, fn=Boolean) => arr.every(fn); ...
- 521我发誓读完本文,再也不会担心Spring配置类问题了
当大潮退去,才知道谁在裸泳.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...
- java 面向对象(三十三):泛型二 泛型在集合中的使用
1. 在集合中使用泛型之前的例子 @Test public void test1(){ ArrayList list = new ArrayList(); //需求:存放学生的成绩 list.add( ...
- ASP.NET Core3.1使用Identity Server4建立Authorization Server-2
前言 建立Web Api项目 在同一个解决方案下建立一个Web Api项目IdentityServer4.WebApi,然后修改Web Api的launchSettings.json.参考第一节,当然 ...
- JavaScript 基础 学习(三)
JavaScript 基础 学习(三) 事件三要素 1.事件源: 绑定在谁身上的事件(和谁约定好) 2.事件类型: 绑定一个什么事件 3.事件处理函数: 当行为发生的时候,要执行哪一个函数 ...
- Apache Avro & Avro Schema简介
为什么需要schema registry? 首先我们知道: Kafka将字节作为输入并发布 没有数据验证 但是: 如果Producer发送了bad data怎么办? 如果字段被重命名怎么办? 如果数据 ...
- Burp Suite Target Module - 目标模块
模块目的之一:获取网站分析 1.从Proxy - HTTP history界面选中需要加入Target Scope的Host 地址,右击,选中Add to Scope. 2.打开Target - Sc ...
- 集训作业 洛谷P1866 编号
这个题是个数学题啊. 总体思路不是很难,每个兔子有一个编号,只要不停的看下一个兔子有多少可选编号,再乘上之前的所有可能性就可以算出一共的编号方法. #include<iostream> # ...
- T133305 57级返校测试重测-T1-数字配对
大致题意: 给定偶数个的数字,操作使得两两配对后的最大值最小. 基本思路: 先排序,然后设i=1,j=n(序列以下标1开始), 每次配对为a[i]+a[j],然后++i,--j. 最后找到最大的配对结 ...
- p72_电子邮件
一.电子邮件格式 信封 abcd@xx.com 内容 2.1 首部(To, Subject)-> (From,To,Subject,Date) 2.2 主体 二.电子邮件组成结构 三.SMTP ...