Fine vs Coarse Locks in Concurrent Programming

slide1 n.w
1 / 68
Embed
Share

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.

  • Concurrent Programming
  • Locks
  • Atomicity
  • Execution Time
  • Processors

Uploaded on | 2 Views


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


  1. atomic lock/unlock

  2. void deposit(Acct account, int amount) { lock(account.lock); int tmp = bank.get(account); tmp += amount; bank.put(account, tmp); unlock(account.lock); } Deposit

  3. 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); } } - - - -

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

  5. public Object get(Object key) { synchronized (myHashMap) { // guards all accesses to hashMap return myHashMap.get(key); } } - -

  6. 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

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

  8. public Object get(Object key) { atomic { // System guarantees atomicity return m.get(key); } }

  9. 1 2 3 4

  10. 1 2 3 4

  11. 1 2 3 4

  12. 1 2 3 4

  13. 1 2 3 4

  14. 1 2 3 4

  15. 1 2 3 4

  16. 1 2 3 4

  17. 1 2 3 4

  18. 1 2 3 4

  19. 1 2 3 4

  20. 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

  21. 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*/ } } }

  22. void transfer(A, B, amount) { atomic { withdraw(A, amount); deposit(B, amount); } }

  23. void transfer(A, B, amount) { synchronized(A) { synchronized(B) { withdraw(A, amount); deposit(B, amount); } } } transfer(x, y, 100); transfer(y, x, 100);

  24. 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); } } }

  25. void transfer(A, B, amount) { atomic { withdraw(A, amount); deposit(B, amount); } } transfer(x, y, 100) transfer(y, x, 100); transfer withdraw deposit 25

  26. atomic -Atomic

  27. #pragma omp transaction { #pragma omp parallel for for (int i=0; i<N; i++) { bin[A[i]]++; } }

  28. // Thread 2 synchronized(lock2) { flagB = true; while (flagA == 0); } // Thread 1 synchronized(lock1) { flagA = true; while (flagB == 0); }

  29. // Thread 1 atomic { ptr = A; } // Thread 2 atomic { ptr = NULL; } atomic { B = ptr->field; }

  30. x 15 X: 10 X: 10 X: 15 X: 10 X: 10 X: 10 X: 15

  31. x 15 X: 15 X: 10 X: 10 X: 15 X: 15 X: 15 X: 10

Related


More Related Content