atomic

c++11 mutex/atomic

谁说我不能喝 提交于 2021-01-13 05:34:14
mutex/atomic c++ basic data struct is not thread-safe, only multithreading read and no write is safe. mutex/atomic tool can solve data race, but if you use them incorrect, it will cause deadlock. mutex basic mutex basic mutex is std::mutex object, very simple rule: call member function lock to lock it, call unlock to unlock it. but you can not lock it when it locked, also not unlock it when it not locked, or it will cause deadlock. std::mutex mtx; mtx.lock(); // ... mtx.unlock(); recursive mutex recursive mutex is std::recursive_mutex object, difference from basic mutex, it will not cause

C++11并发编程:原子操作atomic

懵懂的女人 提交于 2021-01-13 04:28:09
一:概述   项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式。   为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一个线程执行过程中可能被内核临时挂起,这就是线程切换,当内核再次切换到该线程时,之前的数据可能已被修改,不能保证原子操作。   C++11提供了个原子的类和方法atomic,保证了多线程对变量原子性操作,相比加锁机制mutex.locak(),mutex.unlocak(),性能有几倍的提升。   所需头文件<atomic> 二:错误代码 1 // 全局变量 2 int g_num = 0 ; 3 4 void fun() 5 { 6 for ( int i = 0 ; i < 10000000 ; i++ ) 7 { 8 g_num++ ; 9 } 10 return ; 11 } 12 13 int main() 14 { 15 // 创建线程1 16 thread t1(fun); 17 // 创建线程2 18 thread t2(fun); 19 t1.join(); 20 t2.join(); 21 22 cout << g_num << endl; 23 getchar(); 24 return 1 ; 25 } 应该输出结果20000000,实际每次结果都不一样

阿里大牛再写传奇:并发原理JDK源码手册GitHub已破百万

て烟熏妆下的殇ゞ 提交于 2021-01-11 16:11:32
写在前面: 几乎所有的大神都会强调看源码,也强调源码的重要性; 但是如何看源码,源码看什么?看了什么用?看了怎么用? 困扰很多人,尤其是初学者。 由浅入深看源码,探究多线程原理由浅入深看源码,探究多线程原理 本手册的目的在于 基于JDK 7和JDK 8,对整个Concurrent包进行全面的源码剖析。JDK 8中大部分并发功能的实现和JDK 7一样,但新增了一些额外特性。例如CompletableFuture、ConcurrentHashMap的新实现、StampedLock、LongAdder等。对整个Concurrent包的源码进行分析, 看源码,并非学习和总结源码,先能够粗略 的 浏览大量的原码 并保证知晓有这么个东西,有个基本概念,再对其中的设计原理,优化方案进行学习 下面会给大家展示出这本书的部分内容截图和完整目录,需要获取的小伙伴可以直接转发+关注 即可免费获取到这份《Java并发实现原理JDK源码剖析》 我们的口号是:由浅入深看源码,探究多线程原理 目录展示 目录非的长,但是阅读起来需要看那段直接点击目录就可以跳转非常的方便! 部分内容展示: 多线程基础 Atomic类 Lock与ConditionLock与Condition 同步工具类 并发容器 线程池与Future线程池与Future ForkJoinPoolForkJoinPool

再谈synchronized

独自空忆成欢 提交于 2021-01-05 10:23:21
1.synchronized如何实现同步的? 2.synchronized对象锁保存在哪里? 3.如何加锁和释放锁,1.6做了哪些优化? 4.如何将线程挂起等待和唤醒? 对象内置锁 我们 之前 了解过synchronized同步普通方法和静态方法,以及同步代码块,其中的区别在于synchronized具体锁的是那个对象,是具体的实例还是类对象,即class 我们通过反编译:javap -verbose SynchronizedTest.class >sync.txt。可以看出使用synchronized之后的一些细微的变化,方法上的同步关键字,class文件反编译后会发现flags中有ACC_SYNCHRONIZED标识。 1.方法 级的 同步是隐式的:同步方法的常量池中会有一个ACC_SYNCHRONIZED标志。当某个线程要访问某个方法的时候,会检查是否有ACC_SYNCHRONIZED,如果有设置,则需要先获得对象的内置锁,然后开始执行方法,方法执行之后再释放锁。 2.同步代码块:依赖monitorenter和monitorexit相关标识,通过JVM内置锁对象来实现加锁和释放锁。 对象头信息简介:首先每一个对象在堆内存中,实例对象会有对象头信息,对象实体信息,还有填充信息。其中头信息中包括锁状态,GC年纪,Hash值和监控monitor对象的引用等信息

CUDA: Atomic operations on unsigned chars

霸气de小男生 提交于 2021-01-03 02:13:13
问题 I'm a CUDA beginner. I have a pixel buffer of unsigned chars in global memory that can and is updated by any and all threads. To avoid weirdness in the pixel values, therefore, I want to perform an atomicExch when a thread attempts to update one. But the programming guide says that this function only works on 32- or 64-bit words, whereas I just want to atomically exchange one 8-bit byte. Is there a way to do this? Thanks. 回答1: You might implement a critical section using a mutex variable. So

CUDA: Atomic operations on unsigned chars

拟墨画扇 提交于 2021-01-03 02:00:09
问题 I'm a CUDA beginner. I have a pixel buffer of unsigned chars in global memory that can and is updated by any and all threads. To avoid weirdness in the pixel values, therefore, I want to perform an atomicExch when a thread attempts to update one. But the programming guide says that this function only works on 32- or 64-bit words, whereas I just want to atomically exchange one 8-bit byte. Is there a way to do this? Thanks. 回答1: You might implement a critical section using a mutex variable. So

CUDA: Atomic operations on unsigned chars

爷,独闯天下 提交于 2021-01-03 01:59:39
问题 I'm a CUDA beginner. I have a pixel buffer of unsigned chars in global memory that can and is updated by any and all threads. To avoid weirdness in the pixel values, therefore, I want to perform an atomicExch when a thread attempts to update one. But the programming guide says that this function only works on 32- or 64-bit words, whereas I just want to atomically exchange one 8-bit byte. Is there a way to do this? Thanks. 回答1: You might implement a critical section using a mutex variable. So

Java 多线程同步和异步详解

我们两清 提交于 2021-01-02 14:05:47
java线程 同步与异步 线程池 1)多线程并发时,多个线程同时请求同一个资源,必然导致此资源的数据不安全,A线程修改了B线 程的处理的数据,而B线程又修改了A线程处理的数理。显然这是由于全局资源造成的,有时为了解 决此问题,优先考虑使用局部变量,退而求其次使用同步代码块,出于这样的安全考虑就必须牺牲 系统处理性能,加在多线程并发时资源挣夺最激烈的地方,这就实现了线程的同步机制 同步:A线程要请求某个资源,但是此资源正在被B线程使用中,因为同步机制存在,A线程请求 不到,怎么办,A线程只能等待下去 异步:A线程要请求某个资源,但是此资源正在被B线程使用中,因为没有同步机制存在,A线程 仍然请求的到,A线程无需等待 显然,同步最最安全,最保险的。而异步不安全,容易导致死锁,这样一个线程死掉就会导致整个 进程崩溃,但没有同步机制的存在,性能会有所提升 java中实现多线程 1)继承Thread,重写里面的run方法 2)实现runnable接口 Doug Lea比较推荐后者,第一,java没有单继承的限制第二,还可以隔离代码 线程池 要知道在计算机中任何资源的创建,包括线程,都需要消耗系统资源的。在WEB服务中,对于web服 务器的响应速度必须要尽可能的快,这就容不得每次在用户提交请求按钮后,再创建线程提供服务 。为了减少用户的等待时间,线程必须预先创建,放在线程池中

深入浅出zookeeper之一:功能及本质

a 夏天 提交于 2020-12-31 03:32:59
zookeeper(下文简写为zk)大家都不陌生。但是,看到很多同学对zookeeper的理解过于程式化,有些地方甚至需要背,是大可不必的。把本质理解了,概念性和功能介绍都可以推出来的,而且架构要活学活用,透过现象看本质,才能对技术和技术领悟有大的提升。下面来看下zk的功能及本质。 zookeeper的定义及用途 我们先了解官方的定义。 Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination. Apache ZooKeeper 是一个致力于开发和维护开源服务器,该服务器实现高可用的分布式协调框架。 ZooKeeper is a high-performance coordination service for distributed applications. It exposes common services - such as naming, configuration management, synchronization, and group services - in a simple interface so you don't have to write them

AtomicReference原子性引用

人走茶凉 提交于 2020-12-29 23:27:55
AtomicReference > AtomicReference类提供了一个可以原子读写的对象引用变量。 原子意味着尝试更改相同AtomicReference的多个线程(例如,使用比较和交换操作)不会使AtomicReference最终达到不一致的状态。 AtomicReference甚至有一个先进的compareAndSet()方法,它可以将引用与预期值(引用)进行比较,如果它们相等,则在AtomicReference对象内设置一个新的引用。 AtomicStampReference 安全的修改一个变量的值 package com.keytech.task; import org.junit.platform.commons.logging.LoggerFactory; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; /** * @className: AtomicIntegerTest * @description: TODO 类描述 * @author: mac * @date: 2020/12/29 **/ //线程安全 public class AtomicIntegerTest { private static