17
Present by PanPan Lock-Free Algorithms

Lock freealgorithms-130724010300-phpapp02

Embed Size (px)

DESCRIPTION

lock free algo

Citation preview

Page 1: Lock freealgorithms-130724010300-phpapp02

Present by PanPan

Lock-Free Algorithms

Page 2: Lock freealgorithms-130724010300-phpapp02

Time

Thread B

Read a

Calculate a + 1

Save a + 1

Thread A

Read a

Calculate a + 1

Save a + 1

a = a + 1

Read a

Calculate a + 1

Save a + 1

Multi-Thread Programming Problem

Page 3: Lock freealgorithms-130724010300-phpapp02

● Blocking○ Mutex○ Critical Section

● Non-Blocking○ Wait-Free○ Lock-Free○ Obstruction-Free

● Local○ No Shared Data

Multi-Thread Programming Solution

Page 4: Lock freealgorithms-130724010300-phpapp02

Ca

Enter

Enter CDead Dead

TimeDead Lock DeadCritical Section B (Or Close)

void FunctionB()

Enter Critical Section B

Do Something

Lockritical Section A

Lock Return

Do Something

Leave Critical Section A

Leave Critical Section B

void FunctionA()

Enter Critical Section A

Do Something

Lockll FunctionA

Lock

Do Something

Leave Critical Section B

Leave Critical Section A

Dead Lock

Page 5: Lock freealgorithms-130724010300-phpapp02

[1] http://www.justsoftwaresolutions.co.uk/threading/non_blocking_lock_free_and_wait_free.html

Non-Blocking

Obstruction-Free

Lock-Free

Wait-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]

What is Lock-Free?

Page 6: Lock freealgorithms-130724010300-phpapp02

● No "Lock" (Also No Dead Lock)● Better Performance

● Hard to Implement

Advantages and Disadvantages

Page 7: Lock freealgorithms-130724010300-phpapp02

● Use Atom Operation to ModifyCritical Data

● Enter a Spin State When AtomOperation Fail Success?

Yes

Finish

No

Atom Operation

Main Concept

Page 8: Lock freealgorithms-130724010300-phpapp02

InterlockedCompareExchange - Windows API

bool CAS(uint32* pointer, uint32 old_value, uint32 new_value){

if (*pointer == old_value){

*pointer = new_value;return true;

}return false;

}

Compare and Swap

Page 9: Lock freealgorithms-130724010300-phpapp02

● 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

Other Atom Operation

Page 10: Lock freealgorithms-130724010300-phpapp02

{

};

{

public:

Something Wrong

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

}

struct Node

volatile Node* next;

class Stack

volatile Node* head;

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

}

Example - Stack

Page 11: Lock freealgorithms-130724010300-phpapp02

node* old_head = head;

node* next_node = old_head->next;

A

C

B

A

ABA Problem

Head

B

C

Thread A

Pop

A

Head

C

Thread B

Pop

A

B

Head

Push

Head

C

if (head == 0)break;

if (CAS(&head, old_head, next_node))break;

Page 12: Lock freealgorithms-130724010300-phpapp02

● LL/SC○ It would return false when target have

done a "write" action● CAS2○ Add a versioned value

Solute ABA Problem

Page 13: Lock freealgorithms-130724010300-phpapp02

void Stack::

while tru

Example - Stack(fix Pop)class Stack{

volatile Node* head;volatile int version;

public:void Push(Node* node);void Pop();Stack() : head(0), version(0) {}

};

Pop(){

( e){

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

}

Check Version

Add Version

Page 14: Lock freealgorithms-130724010300-phpapp02

● 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

Summary

Page 15: Lock freealgorithms-130724010300-phpapp02

● Toby Jones - Lock-Free Algorithms(Game ProgrammingGems 6)

● Jean-Francois Dube - Efficient and Scalable Multi-CoreProgramming(Game Programming Gems 8)

● Zhou Wei Ming - Multi-core Computing and Programming

Notes and References

Page 16: Lock freealgorithms-130724010300-phpapp02

Any Questions?

Page 17: Lock freealgorithms-130724010300-phpapp02

Thanks for your attentionBy PanPan2010-06-29