Upgrade to Pro — share decks privately, control downloads, hide ads and more …

goroutineとマルチスレッドデザインパターン/ goroutine and multithreaded design pattern

1b7098127bb4872f5fa10415d88479b7?s=47 nametake
December 08, 2016

goroutineとマルチスレッドデザインパターン/ goroutine and multithreaded design pattern

https://connpass.com/event/28621/
Go言語LT大会! 「最近、Go言語始めました」の会での発表資料

1b7098127bb4872f5fa10415d88479b7?s=128

nametake

December 08, 2016
Tweet

Transcript

  1. goroutineͱ ϚϧνεϨου
 σβΠϯύλʔϯ ߦ໦ লޗ

  2. ࣗݾ঺հ • ߦ໦ লޗ(φϝΩ γϣ΢ΰ) • 2016೥౓৽ଔ • Goݴޠྺ 5ϲ݄

  3. goroutine࢖ͬͯ·͔͢?

  4. GoݴޠΛޠΔ্Ͱ ֎ͤͳ͍ฒྻԽͷػೳ

  5. ͜ͷ࢖͍ํ͋ͬͯΔʁ ͢ΕͬͲ͜Θ͍

  6. goroutineͷߟ͑ํͱ σβΠϯύλʔϯ

  7. GoͰͷฒߦॲཧ

  8. υΩϡϝϯτΛॲ෼͢Δ࢓ࣄ υΩϡϝϯτͷࢁ ম٫࿍ Gopher͘Μͱ
 Χʔτ ͨ͘͞Μ͋ΔυΩϡϝϯτΛ೩΍͍ͨ͠

  9. ୯७ͳΞϓϩʔν Gopher͘ΜΛ2ඖ(ਓ?)ʹ

  10. ಉظ଴͕ͪൃੜͯ͠͠·͏ ম٫࿍Λ࢖͑Δͷ͸1౓ʹ1ඖ(ਓ?)

  11. ಉظ଴͕ͪൃੜͯ͠͠·͏ ম٫࿍Λ࢖͑Δͷ͸1౓ʹ1ඖ(ਓ?)

  12. ૿΍ͯ͠΋ޮՌ্͕͕ΓͮΒ͍

  13. GoݴޠͰͷΞϓϩʔν

  14. ฒྻͳॲཧ Gopher͘ΜΛฒྻʹಈ͔͢ͷͰ͸ͳ͘

  15. ฒߦͳॲཧ Gopher͘ΜΛฒߦʹಈ͔͢

  16. ฒߦͳॲཧ υΩϡϝϯτΛΧʔτʹੵΉGopher͘Μ

  17. ฒߦͳॲཧ ΧʔτΛӡͿGopher͘Μ

  18. ฒߦͳॲཧ υΩϡϝϯτΛΧʔτ͔Β͓Ζͯ͠ ೩΍͢Gopher͘Μ

  19. ฒߦͳॲཧ υΩϡϝϯτΛੵΜͰ͍ͨ ΧʔτΛ໭͢Gopher͘Μ

  20. Կ͕خ͍͠ͷ͔

  21. ॲཧΛ଴ͨͳͯ͘ྑ͍

  22. ॲཧΛ଴ͨͳͯ͘ྑ͍ ӡͿͷ͕࢓ࣄ

  23. ॲཧΛ଴ͨͳͯ͘ྑ͍ ೩΍͠ऴΘΔͷΛ ଴ͨͳ͍

  24. ΍Δ͜ͱ͕ͳ͚Ε͹ࢭ·Δ͚ͩ

  25. ͜ΕΒΛ࣮ݱ͢Δػೳ • channel • Gopher͘ΜͷؒΛܨ͍Ͱ͘ΕΔ • ෳ਺ͷGopher͘Μ͕channelΛୣ͍߹͏ͷΛ๷͙ • select •

    ෳ਺ͷGopher͘Μ͔ΒຊΛड͚औΕΔ • mutexͷύοέʔδͱ͔
  26. ϚϧνεϨου σβΠϯύλʔϯ

  27. ϚϧνεϨου
 σβΠϯύλʔϯ • Single Thread Execution • Immutable • Guarded

    Suspension • Balking • Producer-Consumer • Read-Write Lock • Thread-Per-Message • Worker Thread • Future • Two-Phase Termination • Thread-Specific Storage • Active Object
  28. channelͰղܾ • Single Thread Execution • Immutable • Guarded Suspension

    • Balking • Producer-Consumer • Read-Write Lock • Thread-Per-Message • Worker Thread • Future • Two-Phase Termination • Thread-Specific Storage • Active Object
  29. selectͰղܾ • Single Thread Execution • Immutable • Guarded Suspension

    • Balking • Producer-Consumer • Read-Write Lock • Thread-Per-Message • Worker Thread • Future • Two-Phase Termination • Thread-Specific Storage • Active Object
  30. ύοέʔδͰղܾ • Single Thread Execution • Immutable • Guarded Suspension

    • Balking • Producer-Consumer • Read-Write Lock • Thread-Per-Message • Worker Thread • Future • Two-Phase Termination • Thread-Specific Storage • Active Object
  31. ؾΛ͚ͭͳ͍ͱ͍͚ͳ͍͜ͱ

  32. ϚϧνεϨου σβΠϯύλʔϯ • Single Thread Execution • Immutable • Guarded

    Suspension • Balking • Producer-Consumer • Read-Write Lock • Thread-Per-Message • Worker Thread • Future • Two-Phase Termination • Thread-Specific Storage • Active Object
  33. Thread-Per Message • ϝοηʔδΛड͚औΔຖʹεϨουΛཱͯΔ

  34. Thread-Per Message • ϝοηʔδΛड͚औΔຖʹεϨουΛཱͯΔ υΩϡϝϯτΛड͚औͬͨΒ……

  35. Thread-Per Message • ϝοηʔδΛड͚औΔຖʹεϨουΛཱͯΔ ͦΕຖʹGopher͘ΜΛ࡞Δ

  36. Thread-Per Message • ϝοηʔδΛड͚औΔຖʹεϨουΛཱͯΔ

  37. Goͩͱ΍Γ͕ͪ func processHandler(w http.ResponseWriter, r *http.Request) { ~~~όϦσʔγϣϯͱ͔Payloadऔಘͱ͔~~ go Heavy()

    // ͜ͷ෦෼ʂʂʂ w.WriteHeader(http.StatusAccepted) }
  38. Worker Thread • ࠷ॳ͔ΒεϨουΛಈ͔͓ͯ͘͠

  39. Worker Thread • ࠷ॳ͔ΒεϨουΛಈ͔͓ͯ͘͠ υΩϡϝϯτΛ
 ड͚औͬͨΒ…… !

  40. Worker Thread • ࠷ॳ͔ΒεϨουΛಈ͔͓ͯ͘͠ ۭ͍ͯΔ ୭͔͕୲౰

  41. Worker Thread • ࠷ॳ͔ΒεϨουΛಈ͔͓ͯ͘͠ ! ·ͨདྷͨΒ……

  42. Worker Thread • ࠷ॳ͔ΒεϨουΛಈ͔͓ͯ͘͠ ۭ͍ͯΔ ୭͔͕୲౰

  43. Worker Thread • ࠷ॳ͔ΒεϨουΛಈ͔͓ͯ͘͠ Gopher͘Μͷ ਺͸૿͑ͳ͍

  44. channelͩͱ؆୯ func heavy(queue <-chan string) { url := <-queue //

    ϦΫΤετॲཧ } func main() { queue := make(chan string, 32) for i := 0; i < 3; i++ { go heavy(queue) } queue <- “https://google.co.jp” queue <- “https://google.com” // লུ }
  45. ͜ΕͰ΋͏goroutine΋ ා͘ͳ͍ʂʂʂ

  46. Α͖goroutineϥΠϑΛ!!!