SlideShare a Scribd company logo
1 of 17
Download to read offline
Lock-Free Algorithms
Present by PanPan
Multi-Thread Programming Problem
a = a + 1
Read a
Calculate a + 1
Save a + 1
Thread A
Read a
Save a + 1
Calculate a + 1
Thread B
Read a
Save a + 1
Calculate a + 1
Time
Multi-Thread Programming Solution
●Blocking
○Mutex
○Critical Section
●Non-Blocking
○Wait-Free
○Lock-Free
○Obstruction-Free
●Local
○No Shared Data
Dead Lock
void FunctionA()
Enter Critical Section A
Leave Critical Section A
Leave Critical Section B
Enter Critical Section B
void FunctionB()
Enter Critical Section A
Leave Critical Section B
Leave Critical Section A
Enter Critical Section B
Do Something
Do Something
Do Something
Time
Call FunctionA
Return
(Or Close)
Do Something
Dead Lock
Dead Lock
Dead Lock
Dead Lock
What is Lock-Free?
A lock-free data structure is one that doesn't
use any mutex locks. The implication is that
multiple threads can access the data structure
concurrently without race conditions or data
corruption, even though there are no locks —
people would give you funny looks if you
suggested that std::list was a lock-free data
structure, even though it is unlikely that there
are any locks used in the implementation. [1]
Non-Blocking
Obstruction-Free
Lock-Free
Wait-Free
[1] http://www.justsoftwaresolutions.co.uk/threading/non_blocking_lock_free_and_wait_free.html
Advantages and Disadvantages
●No "Lock" (Also No Dead Lock)
●Better Performance
●Hard to Implement
Main Concept
●Use Atom Operation to Modify
Critical Data
●Enter a Spin State When Atom
Operation Fail
Atom Operation
Success?
Finish
No
Yes
Compare and Swap
bool CAS(uint32* pointer, uint32 old_value, uint32 new_value)
{
if (*pointer == old_value)
{
*pointer = new_value;
return true;
}
return false;
}
InterlockedCompareExchange - Windows API
Other Atom Operation
●CAS2 - Compare and Swap 2
●CASN - Compare and Swap N
●DCAS - Double Compare Swap
●MCAS - Multiword Compare and Swap
●LL/SC - Load Linked / Store Conditional
Example - Stack
struct Node
{
volatile Node* next;
};
class Stack
{
volatile Node* head;
public:
void Push(Node* node);
void Pop();
Stack() : head(0) {}
};
void Stack::Push(Node* node)
{
while (true)
{
node->next = head;
if (CAS(&head, node->next, node))
break;
}
}
void Stack::Pop()
{
while (true)
{
node* old_head = head;
if (head == 0)
break;
node* next_node = old_head->next;
if (CAS(&head, old_head, next_node))
break;
}
}
Something Wrong
ABA Problem
Thread A
A
B
C
A
B
C
Head
Pop
Thread B
A
B
C
Pop
C
A
Push
Head
Head
Head
node* old_head = head;
if (head == 0)
break;
node* next_node = old_head->next;
if (CAS(&head, old_head, next_node))
break;
Solute ABA Problem
●LL/SC
○It would return false when target have
done a "write" action
●CAS2
○Add a versioned value
Example - Stack(fix Pop)
void Stack::Pop()
{
while (true)
{
node* old_head = head;
int old_version = version;
if (head == 0)
break;
node* next_node = old_head->next;
if (CAS2(&head, old_head, old_version, next_node, old_version + 1))
break;
}
}
class Stack
{
volatile Node* head;
volatile int version;
public:
void Push(Node* node);
void Pop();
Stack() : head(0), version(0) {}
};
Add Version
Check Version
Summary
●Lock-Free has Better Performance Than Blocking
●Lock-Free has no "Lock"
●Lock-Free is Hard To Implement
●Lock-Free's Spirit - Atom Operation
●Lock-Free's Trap - ABA Problem
Notes and References
●Toby Jones - Lock-Free Algorithms(Game Programming
Gems 6)
●Jean-Francois Dube - Efficient and Scalable Multi-Core
Programming(Game Programming Gems 8)
●Zhou Wei Ming - Multi-core Computing and Programming
Any Questions?
Thanks for your attention
By PanPan
2010-06-29

More Related Content

What's hot

Dynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programsDynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programs
Devexperts
 
Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...
Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...
Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...
Stefan Marr
 

What's hot (20)

Intro to data oriented design
Intro to data oriented designIntro to data oriented design
Intro to data oriented design
 
Task and Data Parallelism
Task and Data ParallelismTask and Data Parallelism
Task and Data Parallelism
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time Checks
 
Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
 
Coroutine
CoroutineCoroutine
Coroutine
 
Parallel computing with GPars
Parallel computing with GParsParallel computing with GPars
Parallel computing with GPars
 
Dynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programsDynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programs
 
Integrating Xtext and Sirius: Strategies and Pitfalls
Integrating Xtext and Sirius: Strategies and PitfallsIntegrating Xtext and Sirius: Strategies and Pitfalls
Integrating Xtext and Sirius: Strategies and Pitfalls
 
Declarative Infrastructure Tools
Declarative Infrastructure Tools Declarative Infrastructure Tools
Declarative Infrastructure Tools
 
AES effecitve software implementation
AES effecitve software implementationAES effecitve software implementation
AES effecitve software implementation
 
SiriusCon 2017 - Integrating Xtext and Sirius: Strategies and Pitfalls
SiriusCon 2017 - Integrating Xtext and Sirius: Strategies and PitfallsSiriusCon 2017 - Integrating Xtext and Sirius: Strategies and Pitfalls
SiriusCon 2017 - Integrating Xtext and Sirius: Strategies and Pitfalls
 
Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...
Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...
Tracing versus Partial Evaluation: Which Meta-Compilation Approach is Better ...
 
Ceph Day Chicago: Using Ceph for Large Hadron Collider Data
Ceph Day Chicago: Using Ceph for Large Hadron Collider Data Ceph Day Chicago: Using Ceph for Large Hadron Collider Data
Ceph Day Chicago: Using Ceph for Large Hadron Collider Data
 
mypipe: Buffering and consuming MySQL changes via Kafka
mypipe: Buffering and consuming MySQL changes via Kafkamypipe: Buffering and consuming MySQL changes via Kafka
mypipe: Buffering and consuming MySQL changes via Kafka
 
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
 
Why Is Concurrent Programming Hard? And What Can We Do about It?
Why Is Concurrent Programming Hard? And What Can We Do about It?Why Is Concurrent Programming Hard? And What Can We Do about It?
Why Is Concurrent Programming Hard? And What Can We Do about It?
 
Clojure - LISP on the JVM
Clojure - LISP on the JVM Clojure - LISP on the JVM
Clojure - LISP on the JVM
 
The Fastest Possible Search Algorithm: Grover's Search and the World of Quant...
The Fastest Possible Search Algorithm: Grover's Search and the World of Quant...The Fastest Possible Search Algorithm: Grover's Search and the World of Quant...
The Fastest Possible Search Algorithm: Grover's Search and the World of Quant...
 
Linux-Permission
Linux-PermissionLinux-Permission
Linux-Permission
 

Similar to Lock free algorithms

Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
Sri Prasanna
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
oscon2007
 

Similar to Lock free algorithms (20)

Parallel Programming: Beyond the Critical Section
Parallel Programming: Beyond the Critical SectionParallel Programming: Beyond the Critical Section
Parallel Programming: Beyond the Critical Section
 
Lockless
LocklessLockless
Lockless
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
The Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldThe Need for Async @ ScalaWorld
The Need for Async @ ScalaWorld
 
Introduction to Concurrent Data Structures
Introduction to Concurrent Data StructuresIntroduction to Concurrent Data Structures
Introduction to Concurrent Data Structures
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmap
 
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
 
PostgreSQL Open SV 2018
PostgreSQL Open SV 2018PostgreSQL Open SV 2018
PostgreSQL Open SV 2018
 
Grokking TechTalk #20: PostgreSQL Internals 101
Grokking TechTalk #20: PostgreSQL Internals 101Grokking TechTalk #20: PostgreSQL Internals 101
Grokking TechTalk #20: PostgreSQL Internals 101
 
Stripe CTF3 wrap-up
Stripe CTF3 wrap-upStripe CTF3 wrap-up
Stripe CTF3 wrap-up
 
cs2110Concurrency1.ppt
cs2110Concurrency1.pptcs2110Concurrency1.ppt
cs2110Concurrency1.ppt
 
CppConcurrencyInAction - Chapter07
CppConcurrencyInAction - Chapter07CppConcurrencyInAction - Chapter07
CppConcurrencyInAction - Chapter07
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 
Operating and Supporting Delta Lake in Production
Operating and Supporting Delta Lake in ProductionOperating and Supporting Delta Lake in Production
Operating and Supporting Delta Lake in Production
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
Lockless Programming GDC 09
Lockless Programming GDC 09Lockless Programming GDC 09
Lockless Programming GDC 09
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Java 7
Java 7Java 7
Java 7
 
H2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleH2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt Dowle
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

Lock free algorithms

  • 2. Multi-Thread Programming Problem a = a + 1 Read a Calculate a + 1 Save a + 1 Thread A Read a Save a + 1 Calculate a + 1 Thread B Read a Save a + 1 Calculate a + 1 Time
  • 3. Multi-Thread Programming Solution ●Blocking ○Mutex ○Critical Section ●Non-Blocking ○Wait-Free ○Lock-Free ○Obstruction-Free ●Local ○No Shared Data
  • 4. Dead Lock void FunctionA() Enter Critical Section A Leave Critical Section A Leave Critical Section B Enter Critical Section B void FunctionB() Enter Critical Section A Leave Critical Section B Leave Critical Section A Enter Critical Section B Do Something Do Something Do Something Time Call FunctionA Return (Or Close) Do Something Dead Lock Dead Lock Dead Lock Dead Lock
  • 5. What is Lock-Free? A lock-free data structure is one that doesn't use any mutex locks. The implication is that multiple threads can access the data structure concurrently without race conditions or data corruption, even though there are no locks — people would give you funny looks if you suggested that std::list was a lock-free data structure, even though it is unlikely that there are any locks used in the implementation. [1] Non-Blocking Obstruction-Free Lock-Free Wait-Free [1] http://www.justsoftwaresolutions.co.uk/threading/non_blocking_lock_free_and_wait_free.html
  • 6. Advantages and Disadvantages ●No "Lock" (Also No Dead Lock) ●Better Performance ●Hard to Implement
  • 7. Main Concept ●Use Atom Operation to Modify Critical Data ●Enter a Spin State When Atom Operation Fail Atom Operation Success? Finish No Yes
  • 8. Compare and Swap bool CAS(uint32* pointer, uint32 old_value, uint32 new_value) { if (*pointer == old_value) { *pointer = new_value; return true; } return false; } InterlockedCompareExchange - Windows API
  • 9. Other Atom Operation ●CAS2 - Compare and Swap 2 ●CASN - Compare and Swap N ●DCAS - Double Compare Swap ●MCAS - Multiword Compare and Swap ●LL/SC - Load Linked / Store Conditional
  • 10. Example - Stack struct Node { volatile Node* next; }; class Stack { volatile Node* head; public: void Push(Node* node); void Pop(); Stack() : head(0) {} }; void Stack::Push(Node* node) { while (true) { node->next = head; if (CAS(&head, node->next, node)) break; } } void Stack::Pop() { while (true) { node* old_head = head; if (head == 0) break; node* next_node = old_head->next; if (CAS(&head, old_head, next_node)) break; } } Something Wrong
  • 11. ABA Problem Thread A A B C A B C Head Pop Thread B A B C Pop C A Push Head Head Head node* old_head = head; if (head == 0) break; node* next_node = old_head->next; if (CAS(&head, old_head, next_node)) break;
  • 12. Solute ABA Problem ●LL/SC ○It would return false when target have done a "write" action ●CAS2 ○Add a versioned value
  • 13. Example - Stack(fix Pop) void Stack::Pop() { while (true) { node* old_head = head; int old_version = version; if (head == 0) break; node* next_node = old_head->next; if (CAS2(&head, old_head, old_version, next_node, old_version + 1)) break; } } class Stack { volatile Node* head; volatile int version; public: void Push(Node* node); void Pop(); Stack() : head(0), version(0) {} }; Add Version Check Version
  • 14. Summary ●Lock-Free has Better Performance Than Blocking ●Lock-Free has no "Lock" ●Lock-Free is Hard To Implement ●Lock-Free's Spirit - Atom Operation ●Lock-Free's Trap - ABA Problem
  • 15. Notes and References ●Toby Jones - Lock-Free Algorithms(Game Programming Gems 6) ●Jean-Francois Dube - Efficient and Scalable Multi-Core Programming(Game Programming Gems 8) ●Zhou Wei Ming - Multi-core Computing and Programming
  • 17. Thanks for your attention By PanPan 2010-06-29