`
standalone
  • 浏览: 596423 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Lock Free Stack

阅读更多
This example is copied from book "Java Concurrency in Practice".
From this example we can learn that the lock free queue code is not correct for MPMC (multi-producer-multi-consumer) in my previous post.

Actually, lock free means you should guarantee the atomicity of updating a pointer. When you add a new node on top of current stack top, the top node might be poped by another thread. So you should prepare to do that again if that did happen. How to do this? We thus need a CAS operation, if current top is what we saw and we have successfully update the top to a new node, ok, our job is done.

While, lock free queue can be more complicated if you insert one node to the tail since we need to update two pointers at same time, one is a *tail* pointing to the last node and one is its *next* pointer.

I will give one correct queue code in the next post.

@ThreadSafe
public class ConcurrentStack <E> {
AtomicReference<Node<E>> top = new AtomicReference<Node<E>>();
public void push(E item) {
Node<E> newHead = new Node<E>(item);
Node<E> oldHead;
do {
oldHead = top.get();
newHead.next = oldHead;
} while (!top.compareAndSet(oldHead, newHead));
}
public E pop() {
Node<E> oldHead;
Node<E> newHead;
do {
oldHead = top.get();
if (oldHead == null)
return null;
newHead = oldHead.next;
} while (!top.compareAndSet(oldHead, newHead));
return oldHead.item;
}
private static class Node <E> {
public final E item;
public Node<E> next;
public Node(E item) {
this.item = item;
}
}
}
分享到:
评论

相关推荐

    A Scalable Lock-free Stack Algorithm (2004)-计算机科学

    A Scalable Lock-free Stack AlgorithmDanny Hendler ∗School of Computer Science Tel-Aviv UniversityTel Aviv, Israel 69978hendlerd@post.tau.ac.ilNir Shavit Tel-Aviv University & Sun ...

    D:\uC_OS-II V2.51源码

    /* Clear the scheduling lock counter */ OSTaskCtr = 0; /* Clear the number of tasks */ OSRunning = FALSE; /* Indicate that multitasking not started */ OSIdleCtr = 0L; /* Clear the 32-bit idle ...

    EurekaLog_7.5.0.0_Enterprise

    20)..Fixed: Minor bugs in stack tracing (which usually affected stacks for leaks) 21)..Fixed: Rare deadlocks in multi-threaded applications 22)..Fixed: Taking screenshot of minimized window 23)..Fixed...

    acpi控制笔记本风扇转速

    Fixed a problem with the Global Lock where the lock could appear to be obtained before it is actually obtained. The global lock semaphore was inadvertently created with one unit instead of zero units....

    汪文君高并发编程实战视频资源全集

    │ 高并发编程第一阶段10讲、Thread构造函数StackSize详细讲解.mp4 │ 高并发编程第一阶段11讲、Thread构造函数StackSize详细讲解-续.mp4 │ 高并发编程第一阶段12讲、Daemon线程的创建以及使用场景分析.mp4 │ ...

    汪文君高并发编程实战视频资源下载.txt

    │ 高并发编程第一阶段10讲、Thread构造函数StackSize详细讲解.mp4 │ 高并发编程第一阶段11讲、Thread构造函数StackSize详细讲解-续.mp4 │ 高并发编程第一阶段12讲、Daemon线程的创建以及使用场景分析.mp4 │ ...

    oracle DBA日常脚本

    ..........\Obj_Lock.sql ..........\Open_Cursors.sql ..........\Pipes.sql ..........\RBS_Extents.sql ..........\RBS_Stats.sql ..........\Recovery_Status.sql ..........\Roles.sql ..........\...

    微软内部资料-SQL性能优化2

    Pages in a processes address space are free, reserved or committed. Reserving memory address space is a way to reserve a range of virtual addresses for later use. If you attempt to access a reserved ...

    VB.NET Developer's Guide(4574).pdf

    Understanding Free Threading 262 SyncLock 263 Summary 265 Solutions Fast Track 265 Frequently Asked Questions 267 Chapter 7 Creating Windows Forms 269 Introduction 270 Application Model 270 ...

    ora分析脚本

    - stack &lt;os_pid&gt; get process stack using oradebug - cursors [all] &lt;match_str&gt;: [all] parsed cursors - sharing &lt;sql_id&gt;: print why cursors are not shared - events [px]: events that someone is ...

    深入解析Oracle.DBA入门进阶与诊断案例

    6.2.8 Library Cache Pin及Library Cache Lock分析 273 6.2.9 诊断案例一:version_count过高造成的Latch竞争解决 281 6.2.10 V$SQL与V$SQLAREA视图 287 6.2.11 Oracle 10g中version_count过高的诊断 292 ...

    BobBuilder_app

    Splitting a page in b+tree has to fix parent nodes and children so effectively will lock the tree for the duration, so parallel updates are very very difficult and have spawned a lot of research ...

    mlib:使用纯C语言(C99或C11)的通用容器和类型安全容器的库,用于容器的广泛收集(与C ++ STL比较)

    mlib:使用纯C语言(C99或C11)的通用容器和类型安全容器的库,用于容器的广泛收集(与C ++ STL比较)

    libcopp:C ++中的跨平台协程库

    libcopp C ++中的跨平台协程库。 Linux + OSX(Clang + GCC) Windows(VC + MinGW) 工作服开发分支建造和单元测试 编译器linux-gcc-4.8 MSVC 14(Visual Studio 2015年) linux-gcc-10 MSVC 15(Visual Studio ...

    c++的跨平台协程库

    c++的跨平台协程库-源码

    BUS Hound

    This field is off by default and can be turned on from the settings Window LOCK 1394 lock transaction NSTS Windows 4 byte kernel mode NTSTATUS field RSET Bus or device reset RSTS ...

    Thinking in Java 4th Edition

    What’s Inside Preface 1 Java SE5 and SE6 .................. 2 Java SE6 ............................................The 4th edition...........................Changes ...........................................

    cc2530_user_guide

    2.3.6 Stack Pointer....................................................................................................... 36 2.4 Instruction Set Summary .................................................

    linux全志R16的linux系统编译的资料_20170502_1655.7z

    全志R16平台编译linux系统V1.0.txt 2017/4/11 13:36 (编译请使用编译android的lichee的选项编译生成的.config文件,不然直接编译会报错!!...rootroot@cm-System-Product-Name:/home/wwt/linux_r16$ tar zxvf ...

Global site tag (gtag.js) - Google Analytics