1 / 101

Programowanie równoległe Część 4 – własne wnioski

Programowanie równoległe Część 4 – własne wnioski. Jakub Binkowski. O mnie. Jakub Binkowski Senior .NET Developer @ Rule Financial Microsoft Most Valuable Professional (MVP) MCP, MCTS, MCPD Lider Łódzkiej Grupy Profesjonalistów IT & .NET jakub@binkowski.com.pl.

willow
Download Presentation

Programowanie równoległe Część 4 – własne wnioski

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programowanie równoległeCzęść 4 – własne wnioski Jakub Binkowski

  2. O mnie • Jakub Binkowski • Senior .NET Developer @ Rule Financial • Microsoft Most Valuable Professional (MVP) • MCP, MCTS, MCPD • Lider Łódzkiej Grupy Profesjonalistów IT & .NET • jakub@binkowski.com.pl

  3. Cykl „Programowanie równoległe w .NET” • Część IWielowątkowość w .NET 4.0 • Część IIInterakcja (synchronizacja) między wątkami, struktury danych i algorytmy równoległe • Część IIIOperacje asynchroniczne • Część IVDobre praktyki, najczęstsze błędy, testowanie

  4. Agenda • Błędy • Dobre praktyki • Ogólne rady • Testowanie

  5. Niepewne założenia

  6. Niepewne założenia var list = newList<int> {1, 3, 4, 5}; Co zwróci instrukcja: • list.Contains(5); wywołana jednocześnie z: • list.Insert(index: 1, item: 2); ?

  7. Niepewne założenia • Czym jest List<T>? length = 4 items = 1 3 4 5 0 1 2 3 4

  8. Niepewne założenia • Jak działa list.Contains(5)? length = 4 items = 1 3 4 5 0 1 2 3 4

  9. Niepewne założenia • Jak działa list.Contains(5)? length = 4 items = FALSE 1 3 4 5 0 1 2 3 4

  10. Niepewne założenia • Jak działa list.Contains(5)? length = 4 items = FALSE 1 3 4 5 0 1 2 3 4

  11. Niepewne założenia • Jak działa list.Contains(5)? length = 4 items = FALSE 1 3 4 5 0 1 2 3 4

  12. Niepewne założenia • Jak działa list.Contains(5)? length = 4 items = TRUE 1 3 4 5 0 1 2 3 4

  13. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 4 5 0 1 2 3 4

  14. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 4 5 0 1 2 3 4

  15. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 4 5 5 0 1 2 3 4

  16. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 4 5 5 0 1 2 3 4

  17. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 4 4 5 0 1 2 3 4

  18. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 4 4 5 0 1 2 3 4

  19. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 1 3 3 4 5 0 1 2 3 4

  20. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 2 1 3 3 4 5 0 1 2 3 4

  21. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 4 items = 2 1 2 3 4 5 0 1 2 3 4

  22. Niepewne założenia • Jak działa list.Insert(index: 1, item: 2)? length = 5 items = 1 2 3 4 5 0 1 2 3 4

  23. Niepewne założeniaList.Insert(1,2) i List.Contains(5) razem length = 4 items = 5? FALSE 1 3 4 5 5 0 1 2 3 4

  24. Niepewne założeniaList.Insert(1,2) i List.Contains(5) razem length = 4 items = 5? FALSE 1 3 4 4 5 0 1 2 3 4

  25. Niepewne założeniaList.Insert(1,2) i List.Contains(5) razem length = 4 items = 5? FALSE 1 3 3 4 5 0 1 2 3 4

  26. Niepewne założeniaList.Insert(1,2) i List.Contains(5) razem length = 4 items = 2 5? FALSE 1 2 3 4 5 0 1 2 3 4

  27. Niepewne założeniaList.Insert(1,2) i List.Contains(5) razem length = 5 items = 1 2 3 4 5 0 1 2 3 4

  28. Niepewne założeniaList.Insert(1,2) i List.Contains(5) razem length = 5 items = List.Contains(5) = false! 1 2 3 4 5 0 1 2 3 4

  29. Dobra praktyka • Nie zakładaj, że jakieś 2 operacje są bezpieczne wielowątkowo • Synchronizuj jednoczesny dostęp: • lock (Monitor) • ReaderWriterLock(Slim) • itp. • Używaj kolekcji bezpiecznych wielowątkowo:System.Collections.Concurrent

  30. Instrukcje (nie)atomowe

  31. Problem: Instrukcje (nie)atomowe jedna linia != jedna instukcja

  32. Co się stało? • i++ to 3 instrukcje: • LOAD @i, R0 • INCREMENT R0 • STORE R0, @i

  33. Co się stało? Pamięć: i = 5 Wątek 1 R0 = 0 i++ Wątek 2 R0 = 0 i++

  34. Co się stało? Pamięć: i = 5 Wątek 1 R0 = 0 i++ Wątek 2 R0 = 0 i++

  35. Co się stało? Pamięć: i = 5 Wątek 1 R0 = 0 i++: • LOAD @i, R0 Wątek 2 R0 = 0 i++: • LOAD @i, R0

  36. Co się stało? Pamięć: i = 5 Wątek 1 R0 = 5 i++: • LOAD @i, R0 Wątek 2 R0 = 5 i++: • LOAD @i, R0

  37. Co się stało? Pamięć: i = 5 Wątek 1 R0 = 6 i++: • LOAD @i, R0 • INC R0 Wątek 2 R0 = 5 i++: • LOAD @i, R0

  38. Co się stało? Pamięć: i = 6 Wątek 1 R0 = 6 i++: • LOAD @i, R0 • INC R0 • STORE R0, @i Wątek 2 R0 = 5 i++: • LOAD @i, R0

  39. Co się stało? Pamięć: i = 6 Wątek 1 R0 = 6 i++: • LOAD @i, R0 • INC R0 • STORE R0, @i Wątek 2 R0 = 6 i++: • LOAD @i, R0 • INC R0

  40. Co się stało? Pamięć: i = 6 Wątek 1 R0 = 6 i++: • LOAD @i, R0 • INC R0 • STORE R0, @i Wątek 2 R0 = 6 i++: • LOAD @i, R0 • INC R0 • STORE R0, @i

  41. Dobra praktyka • Nie zakładaj, że „jedna linia = jedna instrukcja” • Synchronizuj dostęp (lock, itp.) • Używaj gwarantowanych operacji atomowych, np.:Interlocked.Increment(ref_counter);

  42. Zdarzenia (events)

  43. Zdarzenia (events) - przypomnienie • Deklaracja: publiceventEventHandlerSomethingHappened; • Wywołanie: if(SomethingHappened != null) SomethingHappened(this, EventArgs.Empty); • Subskrypcja: SomethingHappened += HandleSomethingHappened; SomethingHappened -= HandleSomethingHappened;

  44. Problem: Zdarzenia a wielowątkowość Czy zdarzenia są bezpieczne wielowątkowo?

  45. Co się stało? SomethingHappened = null Wątek 1 Wątek 2 SomethingHappened += HandleSomethingHappened;

  46. Co się stało? SomethingHappened = HandleSomethingHappened Wątek 1 Wątek 2 SomethingHappened += HandleSomethingHappened;

  47. Co się stało? SomethingHappened = HandleSomethingHappened Wątek 1 Wątek 2 SomethingHappened += HandleSomethingHappened; if (SomethingHappened != null)

  48. Co się stało? SomethingHappened = HandleSomethingHappened Wątek 1 Wątek 2 SomethingHappened += HandleSomethingHappened; if (SomethingHappened != null) true

  49. Co się stało? SomethingHappened = HandleSomethingHappened Wątek 1 Wątek 2 SomethingHappened += HandleSomethingHappened; SomethingHappened-= HandleSomethingHappened; if (SomethingHappened != null)

  50. Co się stało? SomethingHappened = null Wątek 1 Wątek 2 SomethingHappened += HandleSomethingHappened; SomethingHappened-= HandleSomethingHappened; if (SomethingHappened != null)

More Related