SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Lock-Free Algorithms
Present by PanPan
Multi-Thread Programming Problem
Read a
a=a+1

Calculate a + 1
Save a + 1

Thread A

Thread B

Read a
Read a
Calculate a + 1
Save a + 1

Calculate a + 1
Save 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()

void FunctionB()

Enter Critical Section A

Enter Critical Section B

Do Something

Do Something

Dead Lock ll FunctionA
Ca
Dead Lock Critical Section B
Lock
Enter
Do Something

Leave Critical Section B

Dead Lock Critical Section A
Enter
Dead Lock

Return
(Or Close)

Do Something
Leave Critical Section A
Leave Critical Section B

Leave Critical Section A

Time
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
WaitFree

[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
Atom Operation

● Use Atom Operation to Modify
Critical Data
● Enter a Spin State When Atom
Operation Fail

No
Success?

Yes

Finish
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) {}
};

Something Wrong

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;
}
}
ABA Problem
Threa
dA

Threa
dB

Pop

Head

A
B

Pop

A
B

A

Head

C
C

B
C
Head

Push

A

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)
class Stack
{
volatile Node* head;
volatile int version;
public:
void Push(Node* node);
void Pop();
Stack() : head(0), version(0) {}
};

Add Version

void Stack::
Pop()
{

while tru
( e)
{

Check Version
}
}

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;
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

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (9)

3 f3 1_introduction_to_dsp
3 f3 1_introduction_to_dsp3 f3 1_introduction_to_dsp
3 f3 1_introduction_to_dsp
 
Anatomi
AnatomiAnatomi
Anatomi
 
Finalpresentation
FinalpresentationFinalpresentation
Finalpresentation
 
An activity i enjoy doing
An activity i enjoy doingAn activity i enjoy doing
An activity i enjoy doing
 
The Male Mixed Voice
The Male Mixed VoiceThe Male Mixed Voice
The Male Mixed Voice
 
Nomad
NomadNomad
Nomad
 
Hp deskjet 2
Hp deskjet 2Hp deskjet 2
Hp deskjet 2
 
Hinagpis ni florante
Hinagpis ni floranteHinagpis ni florante
Hinagpis ni florante
 
Bronchitis
BronchitisBronchitis
Bronchitis
 

Kürzlich hochgeladen

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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 Nanonetsnaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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...Igalia
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
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.pdfhans926745
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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 MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 Servicegiselly40
 
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.pdfChristopherTHyatt
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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...Martijn de Jong
 

Kürzlich hochgeladen (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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...
 

Lock freealgorithms-130724010300-phpapp02

  • 2. Multi-Thread Programming Problem Read a a=a+1 Calculate a + 1 Save a + 1 Thread A Thread B Read a Read a Calculate a + 1 Save a + 1 Calculate a + 1 Save 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() void FunctionB() Enter Critical Section A Enter Critical Section B Do Something Do Something Dead Lock ll FunctionA Ca Dead Lock Critical Section B Lock Enter Do Something Leave Critical Section B Dead Lock Critical Section A Enter Dead Lock Return (Or Close) Do Something Leave Critical Section A Leave Critical Section B Leave Critical Section A Time
  • 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 WaitFree [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 Atom Operation ● Use Atom Operation to Modify Critical Data ● Enter a Spin State When Atom Operation Fail No Success? Yes Finish
  • 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) {} }; Something Wrong 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; } }
  • 11. ABA Problem Threa dA Threa dB Pop Head A B Pop A B A Head C C B C Head Push A 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) class Stack { volatile Node* head; volatile int version; public: void Push(Node* node); void Pop(); Stack() : head(0), version(0) {} }; Add Version void Stack:: Pop() { while tru ( e) { Check Version } } 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;
  • 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