
Fine vs Coarse Locks in Concurrent Programming
Explore the differences between fine and coarse locks in concurrent programming, delving into their impact on execution time and processor utilization. Learn about atomic operations and their role in ensuring data integrity.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
void deposit(Acct account, int amount) { lock(account.lock); int tmp = bank.get(account); tmp += amount; bank.put(account, tmp); unlock(account.lock); } Deposit
void deposit(Acct account, int amount) { lock(account.lock); int tmp = bank.get(account); tmp += amount; bank.put(account, tmp); unlock(account.lock); } void deposit(Acct account, int amount) { atomic { int tmp = bank.get(account); tmp += amount; bank.put(account, tmp); } } - - - -
public Object get(Object key) { int idx = hash(key); // compute hash HashEntry e = buckets[idx]; // find bucket while (e != null) { // find element in bucket if (equals(key, e.key)) return e.value; e = e.next; } return null; }
public Object get(Object key) { synchronized (myHashMap) { // guards all accesses to hashMap return myHashMap.get(key); } } - -
public Object get(Object key) { int idx = hash(key); HashEntry e = buckets[idx]; while (e != null) { if (equals(key, e.key)) return e.value; e = e.next; } return null; } // compute hash // find bucket // find element in bucket
coarse locks fine locks 1.0000 0.7500 Execution Time 0.5000 0.2500 0.0000 1 2 4 8 16 Processors coarse locks fine locks 5.0000 3.7500 Execution Time 2.5000 1.2500 0.0000 1 2 4 8 16 Processors
public Object get(Object key) { atomic { // System guarantees atomicity return m.get(key); } }
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
coarse locks fine locks TCC 1.0000 HashMap 0.7500 Execution Time 0.5000 0.2500 0.0000 1 2 4 8 16 Processors coarse locks fine locks TCC 4.0000 Balanced Tree 3.0000 Execution Time 2.0000 1.0000 0.0000 1 2 4 8 16 Processors
void transfer(A, B, amount) { synchronized(bank) { try { // What if A invalid or balance too low? withdraw(A, amount); // What if B invalid? deposit(B, amount); } catch(withdraw_exception) { /* undo code 1*/ } catch(deposit_exception) { /* undo code 2*/ } } }
void transfer(A, B, amount) { atomic { withdraw(A, amount); deposit(B, amount); } }
void transfer(A, B, amount) { synchronized(A) { synchronized(B) { withdraw(A, amount); deposit(B, amount); } } } transfer(x, y, 100); transfer(y, x, 100);
void transfer(A, B, amount) { synchronized(A) { synchronized(B) { withdraw(A, amount); deposit(B, amount); } } } void transfer2(A, B, amount) { synchronized(B) { synchronized(A) { withdraw(A, 2*amount); deposit(B, 2*amount); } } }
void transfer(A, B, amount) { atomic { withdraw(A, amount); deposit(B, amount); } } transfer(x, y, 100) transfer(y, x, 100); transfer withdraw deposit 25
#pragma omp transaction { #pragma omp parallel for for (int i=0; i<N; i++) { bin[A[i]]++; } }
// Thread 2 synchronized(lock2) { flagB = true; while (flagA == 0); } // Thread 1 synchronized(lock1) { flagA = true; while (flagB == 0); }
// Thread 1 atomic { ptr = A; } // Thread 2 atomic { ptr = NULL; } atomic { B = ptr->field; }
x 15 X: 10 X: 10 X: 15 X: 10 X: 10 X: 10 X: 15
x 15 X: 15 X: 10 X: 10 X: 15 X: 15 X: 15 X: 10