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

C#-マルチコアプログラミングへの手引き-

 C#-マルチコアプログラミングへの手引き-

78eb99ff73e663e4e375e260a403bc5c?s=128

Es_Program

May 15, 2018
Tweet

Transcript

  1. C# ϚϧνίΞϓϩάϥϛϯάͷखҾ͖

  2. ֓ཁ

  3. ϚϧνίΞͱ͸ ίΞ͸CPUͷத֩෦෼ ίΞ͕2ͭͷCPU͸2ίΞCPU 2ίΞCPU͸෺ཧίΞ਺2 1ίΞΛෳ਺ίΞ(࿦ཧίΞ਺ͰΧ΢ϯτ)ͷΑ͏ʹѻ͏ϋΠ ύʔεϨοσΟϯά͕͋Δ ࿦ཧίΞ਺2ͷ෺ཧίΞΛ2ݸ౥ࡌͨ͠CPU͸࿦ཧίΞ਺4

  4. ಉظ/ඇಉظͱ͸ ෳ਺ͷॲཧΛಉ࣌ʹ࣮ߦ͢Δ͜ͱ ಉظͷ৔߹ɺෳ਺ͷॲཧ͕ڞ௨ͷλ Πϛϯά৴߸ͳͲʹґଘ͢Δ ඇಉظͷ৔߹͸ڞ௨ͷλΠϛϯά৴ ߸౳ʹґଘ͠ͳ͍

  5. ϓϩηε ϓϩάϥϜΛ࣮ߦ͍ͯ͠Δঢ়ଶ ΞϓϦέʔγϣϯ1ݸىಈͯ͠Ε͹1ݸͷϓϩηε͕ಈ ͍ͯΔ ෳ਺ͷϓϩηεΛ࣮ߦ͍ͯ͠Δঢ়ଶ͕Ϛϧνϓϩηε ϚϧνϓϩηεͰͷ݁Ռͷ΍ΓऔΓ͸ग़དྷͳ͍͜ͱ͸ ແ͍͕ҰൠతͰ͸ͳ͍

  6. εϨου ϓϩάϥϜΛ࣮ߦ͍ͯ͠Δͱ͖ͷ࠷খ࣮ߦ୯Ґ 1ͭͷϓϩηε಺ʹ୯ମ·ͨ͸ෳ਺ଘࡏ͢Δ εϨου͕1ͭͳΒγϯάϧεϨου ෳ਺ͳΒϚϧνεϨου ϚϧνεϨουͳΒಉ࣌ʹॲཧΛ࣮ߦͰ͖Δ

  7. εϨουͷར఺ ىಈʹ͔͔ΔΦʔόʔϔου͕খ͍͞ εϨουؒͷ௨৴͕ϓϩηεؒ௨৴ʹൺ΂Ε͹ൺֱత ༰қʹߦ͑Δ OS͕؅ཧ͍ͯ͠Δ ࣮ߦ࣌ʹ͸ϥϯλΠϜ͕ෳ਺ͷεϨουΛੜ੒͍ͯ͠Δ εϨουϓʔϧͷར༻

  8. εϨουϓʔϧ Ұ౓ੜ੒ͨ͠εϨουΛཷΊ͓ͯ͘ͱ͜Ζ εϨου༻ͷΩϡʔʹॲཧΛ౤͛Δͱ͍͋ ͯΔεϨου্͕ख͍۩߹ʹॲཧΛ࣮ߦ͠ ͍ͯͬͯ͘ΕΔ εϨουΛ৽ͨʹੜ੒͠ͳ͍͍ͯͨ͘ΊΦʔ όʔϔου͕ແ͍

  9. εϨουͷ۠ผ جຊతʹεϨουʹछྨ͸ແ͍͕໨తʹΑͬ ໊ͯલ͕͍͍ͭͯΔ ΞϓϦέʔγϣϯͰUIΛ੍ޚ͢ΔεϨουΛ UIεϨουͱ͍͏(UIͷมߋ͕Մೳ) UIεϨου͔Βىಈͨ͠εϨουΛϫʔΧʔε Ϩουͱ͍͏(UIͷมߋ͕ग़དྷͳ͍)

  10. ϑΥΞάϥ΢ϯυεϨου .NETͷϓϩάϥϜͰ௚઀ThreadΫϥεΛੜ ੒͢ΔͱɺϑΥΞάϥ΢ϯυεϨουͱͳΔ ෳ਺ͷϑΥΞάϥ΢ϯυεϨου͕͋ͬͨ৔ ߹ɺશͯͷϑΥΞάϥ΢ϯυεϨουॲཧ͕ ׬ྃ͢Δ·ͰΞϓϦέʔγϣϯ͸ऴྃ͠ͳ͍

  11. όοΫάϥ΢ϯυεϨου ThreadΫϥεΛੜ੒͠ɺIsBackgroundϓϩύςΟΛ falseʹ͢Δ(͋Δ͍͸ผͷํ๏ͰεϨουΛੜ੒͢Δ) ͱϑΥΞάϥ΢ϯυεϨουͱͳΔ(نఆ஋͕false) ϑΥΞάϥ΢ϯυεϨου͕શͯऴྃͨ࣌͠఺Ͱɺ όοΫάϥ΢ϯυεϨουͷॲཧ͕࢒͍ͬͯͯ΋Ξ ϓϦέʔγϣϯ͸ڧ੍ऴྃ͢Δ εϨουϓʔϧ͸όοΫάϥ΢ϯυεϨου

  12. λεΫ εϨουʹ͔ΘΔ৽͍֓͠೦ ศརͰ൚༻ੑ͕ߴ͍ هड़͕؆ܿͰΘ͔Γ΍͍͢ ࢖͑ΔͳΒ͹ͳΔ΂͘λεΫΛ༻͍Δ

  13. εϨουͷॲཧ׬ྃΛ஌Δʹ͸

  14. ϙʔϦϯά εϨουॲཧΛߦ͏ϝιουΛ࣋ͭΫ ϥεʹɺॲཧ͕׬͍ྃͯ͠Δ͔Λද͢ ϑϥάΛ࣋ͨͤɺ؅ཧ͓ͤͯ͘͞ ॲཧ͕׬͍ྃͯ͠Δ͔Ͳ͏͔͸ɺஞ࣍ ϑϥάΛ֬ೝ͢Ε͹Α͍

  15. Πϕϯτ௨஌ ओʹεϨουͰͷॲཧͷޙʹಛఆͷॲཧΛ࣮ߦ ͍ͨ࣌͠(tryʹର͢ΔfinallyͷΑ͏ͳ΋ͷ)ʹ࢖͏ ॲཧ׬ྃޙʹΠϕϯτΛݺͼग़͢ߏ଄ʹ͓ͯ͠ ͘ ֎͔ΒΠϕϯτΛ௥Ճ͢Δ͜ͱͰॲཧ׬ྃ࣌ʹ ࣗಈతʹͦͷΠϕϯτΛ࣮ߦͯ͘͠ΕΔ

  16. Wait ࣮ߦ͍ͯ͠Δॲཧ͕ऴͬͯ͘ΕΔ·Ͱɺ ϝΠϯͷεϨουͰ͸εϨουຖʹ༻ҙ ͞Ε͍ͯΔWaitΛ࣮ߦ͢Δ ͜͏͢Δ͜ͱͰɺWaitͰࢦఆͨ͠εϨο υ͕ऴྃ͢Δ·Ͱ͸ॲཧΛࢭΊ͓ͯ͘ࣄ ͕ग़དྷΔ

  17. FCLͷ༷ʑͳεϨουॲཧ

  18. 1. ࢖͏ͷ͸UIεϨουʁϫʔΧʔεϨουʁ 2. εϨουͷ݁Ռ͸Ͳ͏΍ͬͯड͚औΔʁ 3. εϨουॲཧޙʹίʔϧόοΫϝιου(try ʹର͢ΔfinallyΈ͍ͨͳ΋ͷ)Λ࣮ߦͰ͖ Δʁ 4. ίʔϧόοΫϝιουͰεϨουॲཧޙͷ

    ݁ՌΛड͚औΕΔʁ Question
  19. ThreadΫϥεͷجຊ ͜ͷํ๏͚͕ͩεϨουϓʔϧΛ࢖Θͳ͍ Thread t = new Thread(new ThreadStart(ॲཧ));ͱ͍͏ײ͡ͰεϨουΛ࡞Δ t.Start();ͰεϨου։࢝ t.Join();ͰεϨουͷ׬ྃ·ͰݱࡏͷεϨουΛࢭΊΔ

    t.Abort();ͰॲཧΛଧͪ੾Δ(ThreadAbortExceptionΛൃੜͤ͞Δ) IsBackgroundͰϑΥΞ/όοΫάϥ΢ϯυΛมߋՄೳ IsBackgroundͷنఆ஋͸falseͰϑΥΞάϥ΢ϯυεϨου
  20. Answer -Thread- 1. UIεϨουͰ΋ϫʔΧʔεϨουͰ΋ࢦఆՄೳ 2. ड͚औΔखஈ͸ඪ४Ͱ༻ҙ͞Ε͍ͯͳ͍ɻThreadΛར༻͢Δ ΫϥεͷϑΟʔϧυͳͲΛར༻ͯ͠ڞ༗͢ΔͳͲ͔͠खஈ͸ແ ͍ 3. ඪ४Ͱ͸༻ҙ͞Ε͍ͯͳ͍ɻऴྃλΠϛϯάΛϙʔϦϯά/Π

    ϕϯτͳͲͰݕग़ͯࣗ͠෼ͰίʔϧόοΫϝιουΛݺͿ 4. ThreadͰ໭Γ஋Λαϙʔτ͍ͯ͠ͳ͍ͷͰແཧɻThreadΛར ༻͢ΔΫϥεͷϑΟʔϧυͳͲΛར༻ͯ͠ڞ༗͢Δ
  21. ThreadPoolͷجຊ bool result = ThreadPool.QueueUserWorkItem(new WaitCallback(ॲཧ),state);ͰΩϡʔʹॲཧΛૹΕΔ state͸ObjectܕͳͷͰɺ޷͖ͳҾ਺Λϥοϓͨ͠Ϋϥ εͳͲΛ౉ͯ͠ॲཧͰΩϟετ͠ɺ࢖ͬͨΓͰ͖Δ ໭Γ஋͸Ωϡʔʹ഑ஔͰ͖ͨΒtrue(͜ͷ໭Γ஋ʹҙຯ ͸͋·Γແ͍ͷͰࣺͯͯ΋OK)

    ॲཧ׬ྃΛ଴ͬͨΓڧ੍తʹதஅ͢Δ͜ͱ͸ग़དྷͳ͍
  22. Answer -ThreadPool- 1. ৗʹϫʔΧʔεϨου 2. Threadͱಉ༷ 3. Threadͱಉ༷ 4. Threadͱಉ༷

  23. Threadͱͷҧ͍ ৗʹϫʔΧʔεϨου ThreadͰ͸৽͍͠εϨουΛੜ੒͢Δ͕ɺ ThreadPoolͰ͸طʹଘࡏ͢ΔεϨουΛ্ख͘ར༻ ͯ͘͠ΕΔͷͰεϨουੜ੒ͷΦʔόʔϔου͕ແ ͍ ϫʔΧʔεϨουͰ͍͍ͷͳΒͪ͜ΒΛੵۃతʹར ༻͢Δͱ͍͍

  24. delegateΛ࢖͏ delegateܕ͸ίϯύΠϧ࣌ʹࣗಈͰBeginInvoke/EndInvoke͕࣮૷͞ΕΔ BeginInvokeΛ࣮ߦ͢ΔͱεϨουϓʔϧͷΩϡʔʹϝιουΛ౉ͤΔ Ҿ਺ʹ͸ॲཧ࣮ߦޙʹ࣮ߦͯ͠΄͍͠ॲཧ(ίʔϧόοΫϝιου)ͱͦ ͷॲཧʹඞཁͳҾ਺ ίʔϧόοΫϝιουͰॲཧ݁ՌΛར༻͍ͨ͠৔߹͸EndInvokeϝιο υΛ࢖͏(ίʔϧόοΫͰ݁ՌΛඞཁͱ͠ͳ͍ͷͳΒEndInvokeϝιου ͸ෆཁͰ͋Δ) εϨουॲཧͷ׬ྃΛ଴ͭʹ͸໭Γ஋ͷIAsyncResultΛ࢖͑Δ

  25. Answer -delegate- 1. ৗʹϫʔΧʔεϨου(BeginInvoke͸εϨουϓʔϧʹ ॲཧΛ౤͛Δ) 2. BeginInvokeͷ݁ՌIAsyncResultΛ༻͍Δ 3. BeginInvokeͷୈೋୈࡾҾ਺͕̺ʔϧόοΫϝιουؔ܎ 4.

    BeginInvokeͷୈೋҾ਺ʹࢦఆͨ͠ϝιουͰEndInvoke Λ࣮ߦ͠ɺద੾ͳܕʹΩϟετ͢Δ͜ͱͰ݁ՌΛಘΒΕ Δ
  26. BackgroundWorkerΛ࢖͏ ϑΥʔϜ༻ʹಋೖ͞Εͨ εϨουͰॲཧ͍ͤͨ͞ίʔυ͸DoWorkΠϕϯτϋϯυ ϥʔʹ௥Ճ͢Δ RunWorkerAsyncͰεϨουΩϡʔʹॲཧΛૹΔ εϨουͷॲཧ͕׬ྃ͢ΔͱRunWorkerCompleteΠϕϯτ͕ ൃੜ͢Δ(͜ͷΠϕϯτ͸UIεϨουͰ࣮ߦ͞ΕΔ) CancelAsyncͰॲཧதஅΛཁٻͰ͖Δ

  27. Answer -BackgroundWorker- ϫʔΧʔεϨου ඪ४Ͱ͸උΘ͍ͬͯͳ͍ RunWorkerCompletedΠϕϯτʹొ࿥͢Ε͹Α ͍ ΠϕϯτϋϯυϥʔͷҾ਺Λ্ख͘࢖͑͹͍͚ Δ

  28. EAPΛࣗ਎Ͱ࡞Δ System.ComponentModel໊લۭؒͷ ComponentΫϥεΛܧঝͯ͠ EAP(Event-based Asynchronous Pattern) ͷઃܭʹैͬͨಠࣗͷΫϥεΛ࡞ΕΔ ࠶ར༻ͯ͠΋Β͏෦඼ͳͲʹ࢖͏

  29. TaskΛ࢖͏ Task t = new Task(ॲཧ); t.Start();͜ΕͰॲཧΛελʔτग़དྷΔ Task t =

    Task.Run(ॲཧ);্2ߦΛ·ͱΊΒΕΔ Task t = new Task.Factory.StartNew(ॲཧ,Φϓγϣϯ);ͰΦϓγϣϯΛࢦఆͯ͠λεΫ Λ࡞੒࣮͠ߦͰ͖Δ t.IsCompletedͰॲཧ͕׬͔ྃͨ͠Ͳ͏͔ௐ΂ΒΕΔ(ϙʔϦϯάͰ࢖͑Δ) t.Wait();ͰλεΫͷॲཧऴྃΛ଴ػͰ͖Δ Task.WaitAll(Մม௕ͰλεΫΛࢦఆ);੩తϝιουͰࢦఆͨ͠λεΫશͯͷऴྃΛ଴ػ Ͱ͖Δ
  30. Answer -Task- 1. ϫʔΧʔεϨου 2. ໭Γ஋ͱͯ͠αϙʔτ͍ͯ͠Δ 3. ContinueWithϝιουΛνΣʔϯ͢Δ ͜ͱͰ؆୯ʹՄೳʹͳΔ 4.

    ؆୯ʹՄೳ
  31. εϨου͔Β໭Γ஋ΛಘΔ

  32. Thread/ThreadPool ໭Γ஋ͱͯ͠͸αϙʔτ͞Ε͍ͯͳ͍ ϑΟʔϧυͳͲΛڞ༗ͯ͠࢖͏ࣄͰ஋ ͷड͚౉͠Λ࣮ݱ͢Δ͔͠ͳ͍

  33. delegate BeginInvoke/EndInvokeϝιουΛ࢖༻͢Δ͜ͱͰ໭Γ஋Λऔಘ Ͱ͖Δ ໭Γ஋IAsyncResultܕ͸AsyncResultʹΩϟετͰ͖Δ AsyncResult.AsyncDelegate͸εϨουϓʔϧʹ౤͛ͨdelegate ʹΩϟετͰ͖Δ BeginInvokeϝιουͷ໭Γ஋͔Β͸࠷ऴతͳ݁ՌΛɺ EndInvokeϝιου͸ίʔϧόοΫϝιου಺Ͱ࢖༻͢Δ͜ͱ Ͱܧଓॲཧ͢ΔͨΊͷલͷॲཧͷ݁ՌΛಘΔ͜ͱ͕Ͱ͖Δ

  34. BackgroundWorker εϨουͰ࣮ߦ͞ΕΔϝιου͔Β௚઀஋͸ฦͤͳ ͍ DoWorkΠϕϯτϋϯυϥʔͷҾ਺DoWorkEventArgs ͷResultϓϩύςΟʹฦ͍ͨ͠஋ΛηοτͰ͖Δ RunWorkerCompletedΠϕϯτϋϯυϥʔͰ RunWorkerCompletedEventArgsͷResultϓϩύςΟ Λࢀর͢Δ͜ͱͰ஋ΛऔಘͰ͖Δ

  35. Task ໭Γ஋Λαϙʔτ͍ͯ͠Δ TaskΫϥεͷδΣωϦοΫ൛ʹฦ͍ͨ͠Ҿ਺Λࢦఆͯ͠TaskΛ࡞੒ ͠࢖͏ Task<int> t = new Task<int>(ॲཧ); t.Start();

    int result = t.Result; ResultϓϩύςΟΛ࢖༻ͨ࣌͠఺Ͱɺݺͼग़͠ଆͷεϨου͸݁Ռ ͕ฦͬͯ͘Δ·Ͱ଴ػ͢Δ
  36. εϨουͰॲཧ׬ྃޙʹ ଓ͚ͯผͷॲཧΛͤ͞Δ(ίʔϧόοΫ)

  37. Thread/ThreadPool ͜ΕΒͷΫϥεͰ͸εϨουॲཧ׬ ྃޙʹଓ͚ͯผͷॲཧΛ͢Δ࢓૊Έ ͸༻ҙ͞Ε͍ͯͳ͍ εϨουͷ׬ྃΛ଴ͬͯޙଓͷॲཧ Λ࣮ߦͤ͞Δඞཁ͕͋Δ

  38. delegate delegateͷBeginInvokeϝιουͷҾ਺ʹ͸ίʔϧόοΫ ϝιου͕౉ͤΔ εϨουͷॲཧ͕׬ྃ͢ΔͱίʔϧόοΫ͕ݺ͹ΕΔ ͜ͷίʔϧόοΫϝιου͸AsyncCallbackσϦήʔτ ʹҰக͢ΔγάωνϟΛ࣋ͭඞཁ͕͋Δ ίʔϧόοΫϝιου͸ϫʔΧʔεϨουͰ࣮ߦ͞ΕΔ ͷͰUIΛ௚઀ߋ৽Ͱ͖ͳ͍

  39. BackgroundWorker εϨουͰͷॲཧ͕׬ྃ͢ΔͱRunWorkerCompletedΠϕϯτ ͕ݺ͹ΕΔ ͜ͷΠϕϯτϋϯυϥʹॲཧΛࢦఆ͓͚ͯ͠͹ܧଓ࣮ߦ͞ΕΔ ͜ͷΠϕϯτϋϯυϥʔʹ౉͞ΕΔ RunWorkerCompletedEventArgsͷResultϓϩύςΟ͔Βલͷॲ ཧͷ݁ՌΛಘΔ͜ͱ͕Ͱ͖Δ UIεϨουͰ࣮ߦ͞ΕΔ

  40. EAP EAPͰ͸εϨουͷ׬ྃ࣌ʹ DoWorkCompletedΠϕϯτΛݺͼग़ ͢Α͏ʹ࡞Δ UIεϨουͰ࣮ߦ͞ΕΔ

  41. Task(ܧଓλεΫ) ContinueWithϝιουΛ࢖͏͜ͱͰॲཧΛܧଓ࣮ߦͤ͞Δ͜ͱ͕Ͱ͖ Δ ResultϓϩύςΟΛ࢖͏͜ͱͰ݁ՌΛಘΔ͜ͱ΋ग़དྷΔ ResultϓϩύςΟ࢖༻࣌ʹݺͼग़͠ଆͷεϨου͸ॲཧ׬ྃ·Ͱϒ ϩοΫ͞ΕΔ Task<string> t = Task.Run((

    ) => ॲཧ).ContinueWith<String>(_t => ॲཧ(_t.Result)); string result = t.Result;
  42. Thread

  43. using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.Remoting.Messaging;
 
 namespace

    ConsoleProject
 {
 class MainClass
 {
 public static void Main(string[] args)
 {
 Thread t = new Thread(new ThreadStart(Show));
 t.Start();
 Console.WriteLine("Main Method called.");
 t.Join();
 Console.WriteLine("exit...");
 }
 
 public static void Show()
 {
 Thread.Sleep(100);
 Console.WriteLine("Show Method called.");
 }
 }
 }

  44. using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.Remoting.Messaging;
 
 namespace

    ConsoleProject
 {
 class MainClass
 {
 static int x = 100;
 
 public static void Main(string[] args)
 {
 //2ͭͷεϨουͰڞ௨ͷϑΟʔϧυ(ࢿݯ)Λར༻͢Δ
 Thread t1 = new Thread(new ThreadStart(ShowAndIncrement));
 t1.Start();
 Thread t2 = new Thread(new ThreadStart(Show));
 t2.Start();
 
 //ऴྃ଴ͪ
 t1.Join();
 t2.Join();
 Console.WriteLine("exit...");
 }
 
 public static void ShowAndIncrement()
 {
 Thread.Sleep(100);
 Console.WriteLine("ShowAndIncrement Method called. ref value = { 0}", x++);
 }
 
 public static void Show()
 {
 Console.WriteLine("Show Method called. ref value = { 0}", x);
 }
 }
 } ShowAndIncrement͸͕࣌ؒ ͔͔ΔॲཧͳͷͰɺ͜ΕͰ͸ ࢥ͏Α͏ͳॱ൪Ͱ࣮ߦ͞Εͳ͍ Thread͸໭Γ஋Λαϙʔτ͍ͯ͠ͳ͍ͷͰ ϑΟʔϧυΛ࢖ͬͯڞ༗͢Δ ThreadStart͸Ҿ਺΋ αϙʔτ͞Εͯͳ͍
  45. using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.Remoting.Messaging;
 
 namespace

    ConsoleProject
 {
 class MainClass
 {
 static int x = 100;
 
 public static void Main(string[] args)
 {
 //2ͭͷεϨουͰڞ௨ͷϑΟʔϧυ(ࢿݯ)Λར༻͢Δ
 Thread t1 = new Thread(new ThreadStart(ShowAndIncrement));
 t1.Start();
 t1.Join();//ऴྃΛ଴ͭ
 Thread t2 = new Thread(new ThreadStart(Show));
 t2.Start();
 
 //ऴྃ଴ͪ
 t2.Join();
 Console.WriteLine("exit...");
 }
 
 public static void ShowAndIncrement()
 {
 Thread.Sleep(100);
 Console.WriteLine("ShowAndIncrement Method called. ref value = { 0}", x++);
 }
 
 public static void Show()
 {
 Console.WriteLine("Show Method called. ref value = { 0}", x);
 }
 }
 } ͜ΕͳΒ͏·͍͕͘͘ JoinͰॲཧΛ଴ͭ࣌ؒ ͕ඞཁʹͳΔ
  46. ThreadPool

  47. using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.Remoting.Messaging;
 
 namespace

    ConsoleProject
 {
 class MainClass
 {
 //ϙʔϦϯά͢ΔͨΊͷ࢓ֻ͚
 static bool endFunc = false;
 
 public static void Main(string[] args)
 {
 int x = 100;
 //Ωϡʔʹ഑ஔग़དྷͳ͔ͬͨ৔߹ʹfalse͕ฦͬͯ͘Δ͕΄΅ແҙຯͳͷͰී௨͸औಘ͠ͳͯ͘΋͍͍
 //ԿނͳΒɺ഑ஔग़དྷͳ͔ͬͨ৔߹͸ྫ֎͕ૹग़͞ΕΔ͔Β
 bool qurResult = ThreadPool.QueueUserWorkItem(new WaitCallback(Show), x);
 //εϨουϓʔϧͷॲཧ͕ऴ͔ྃͨ͠Λ൑ఆ͢Δ࢓ֻ͚͸ඪ४Ͱ͸༻ҙ͞Ε͍ͯͳ͍
 //ඞཁͳΒϙʔϦϯά͢Δ࢓ֻ͚Λࣗ෼Ͱ༻ҙ͢Δ
 while (!endFunc)
 {
 //ॲཧ͕ऴΔ·ͰϙʔϦϯά
 }
 Console.WriteLine("exit...");
 }
 
 public static void Show(object x)
 {
 Console.WriteLine(x);
 Thread.Sleep(2000);//2ඵ͔͔Δॲཧ
 endFunc = true;
 }
 }
 } ීஈ͸QueueUserWorkItem ͷ໭Γ஋͸ࣺͯͯΑ͍ ϙʔϦϯάͷखஈ͕༻ҙ ͞Ε͍ͯͳ͍ͨΊࣗ෼Ͱ ࣮૷͢Δඞཁ͕͋Δ WaitCallbackσϦήʔτͷ γάωνϟʹैͬͨϝιου
  48. delegate

  49. using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.Remoting.Messaging;
 
 namespace

    ConsoleProject
 {
 class MainClass
 {
 public static void Main(string[] args)
 {
 Action<int> act;
 act = n => Console.WriteLine(n);
 act.BeginInvoke(10, null, null);
 Console.ReadKey();
 }
 }
 }

  50. using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.Remoting.Messaging;
 
 namespace

    ConsoleProject
 {
 class TestDelegateAsync
 {
 
 class MainClass
 {
 public static void Main(string[] args)
 {
 TestDelegateAsync test = new TestDelegateAsync();
 TestDelegate d = test.DoWork;
 d.BeginInvoke(test.CallbackMethod, null);
 Console.ReadKey();
 }
 
 }
 
 public delegate int TestDelegate();
 
 //࣮ߦ͍ͨ͠ॲཧ
 public int DoWork()
 {
 Console.WriteLine("DoWork");
 return 100;
 }
 
 //AsyncCallbackͷγάωνϟʹैͬͨίʔϧόοΫϝιου
 public void CallbackMethod(IAsyncResult ar)
 {
 //BeginInvokeͷ݁ՌΛ࢖ͬͯɺͦͷεϨου͔Β݁ՌΛநग़ͯ͠දࣔ
 TestDelegate @delegate = (TestDelegate)((AsyncResult)ar).AsyncDelegate;
 int result = @delegate.EndInvoke(ar);
 Console.WriteLine(result);
 }
 }
 }
 ࣮ߦ͍ͨ͠ॲཧ͕ೖΔσϦήʔτΛ༻ҙͯ͠ ࣮ߦ͍ͨ͠ॲཧΛॻ͘ γάωνϟʹैͬͨίʔϧόοΫϝιου ͜ͷΑ͏ʹͯ݁͠ՌΛಘΒΕΔ
  51. Task

  52. namespace ConsoleProject
 {
 class MainClass
 {
 public static void Main(string[]

    args)
 {
 Console.WriteLine("Hello World!");
 var a = new TestAsync();
 //10͔Β1·Ͱදࣔ͢Δ͕
 //ঢ়گʹԠͯ͡දࣔॱ͕มΘΔ
 for (int i = 10; i > 0; --i)
 a.Func(i);
 Console.ReadKey();
 }
 }
 
 class TestAsync
 {
 public void Func(int x)
 {
 Task.Run(() =>
 {
 Thread.Sleep(100 * x);
 Console.WriteLine(x++);
 });
 }
 }
 }
 جຊతͳ࢖͍ํ Ҿ਺Λදࣔͯ͠Ճࢉ͢Δ ॏ͍ͨॲཧ
  53. ͔͠͠ɺ͜ͷํ๏ͩͱϓϩάϥϜ͕࣮ߦͯ͠ εϨουॲཧ͕ऴͬͯͳ͍ঢ়ଶͰ ΩʔΛೖྗͯ͠΋ ॲཧ్தͰڧ੍ऴྃ͞Εͯ͠·͏ ͜ΕΛղܾ͢ΔͨΊʹ ϝΠϯεϨουͰWait͢ΔͱҎԼͷΑ͏ʹͳΔ

  54. namespace ConsoleProject
 {
 class MainClass
 {
 public static void Main(string[]

    args)
 {
 Console.WriteLine("Hello World!");
 var a = new TestAsync();
 //10͔Β1·Ͱදࣔ͢Δ͕
 //ঢ়گʹԠͯ͡දࣔॱ͕มΘΔ
 Task[] tasks = new Task[10];
 for (int i = 10; i > 0; --i)
 tasks[i - 1] = a.Func(i);
 Task.WaitAll(tasks);
 Console.WriteLine("End All Tasks.");
 }
 }
 
 class TestAsync
 {
 public Task Func(int x)
 {
 return Task.Run(() =>
 {
 Thread.Sleep(100 * x);
 Console.WriteLine(x++);
 });
 }
 }
 } Runͷ໭Γ஋͸TaskܕͰ͋Δ (͖ͬ͞ͷϓϩάϥϜͰ͸ແࢹࣺͯͯͯͨ͠) શͯͷλεΫ(໭Γ஋Ͱಘͨ) ͕ऴྃ͢Δ·Ͱ଴ͭ
  55. namespace ConsoleProject
 {
 class MainClass
 {
 public static void Main(string[]

    args)
 {
 var a = new TestAsync();
 a.Func(100);
 }
 }
 
 class TestAsync
 {
 public void Func(int x)
 {
 var t = Task<int>.Run(() => x) .ContinueWith(a => Console.WriteLine(a.Result));
 t.Wait();
 }
 }
 } ໭Γ஋intͷॲཧΛͯ͠ɺͦͷ໭Γ஋Λ࢖༻ͨ͠ ίʔϧόοΫϝιουݺͼͩ͠ͷྫ ContinueWithϝιουͷҾ਺ʹೖ͖ͬͯͨ Runϝιουͷ໭Γ஋(Task<int>ܕ)ͷ ResultϓϩύςΟΛ࢖͑͹؆୯ʹ ίʔϧόοΫϝιουʹ஋ΛҾ͖౉ͤΔ
  56. async/await

  57. ໾ׂ TaskͷܧଓλεΫΛ؆қతʹߦ͏ͨΊͷߏจ ϝιου୯ҐͰܧଓεϨουΛ؆୯ʹهड़ग़དྷ ΔΑ͏ʹͳΔ ܧଓॲཧʹલͷॲཧ݁ՌΛ౉͢͜ͱ΋༰қ ܧଓॲཧ͚ͩͰͳ͘ɺผͷεϨουͰߦͬͯ΄ ͍͠ϝιουͷهड़ͷΑ͏ͳ࢖͍ํ΋ग़དྷΔ

  58. async/awaitͷجຊ await͸म০ͨ͠Task<T>ܕΦϒδΣΫτͷඇಉظॲཧ͕׬ྃͨ͠Βɺͦͷ ݁ՌͷTܕม਺Λฦ͢ awaitͰम০ͨ͠จҎ߱ͷॲཧ͸ͦͷจ͕ऴྃͨ͠Β࣮ߦ͞ΕΔ awaitͰम০ͨ͠จͷ໭Γ஋Task<T>͸Tܕͷ݁Ռͱͯ͠ม׵͞ΕΔ async͸ϝιου಺ͰawaitΛ࢖͏͜ͱΛએݴ͢Δ async͸໭Γ஋ͱͯ͠return͞ΕͨTܕม਺ΛTask<T>ܕʹม׵͢Δ ϝιουͷ໭Γ஋͸void/Task(͜ͷ2ͭ͸ඇಉظॲཧͰͷ໭Γ஋ແ͠)/ Task<T>ܕͷ͍ͣΕ͔

  59. using System;
 using System.Threading.Tasks;
 using System.Threading;
 
 class async_await
 {


    static void Main(string[] args)
 {
 var test = new AsyncTest();
 test.Test1();
 Console.ReadKey();
 }
 }
 
 class AsyncTest
 {
 //͕͔͔࣌ؒΔͨΊɺผεϨουͰ࣮ߦͯ͠΄͍͠ॲཧ
 //(ผεϨουͰߦͬͯ΋ଞʹѱӨڹΛ༩͑ͳ͍લఏ)
 public async void Test1()
 {
 await Task.Run(() =>
 {
 Thread.Sleep(2000);
 Console.WriteLine("Test1ॲཧA");
 });
 }
 } ͜ͷ··ͩͱॲཧऴྃલʹ Կ͔ΩʔΛԡ͢ͱڧ੍ऴྃ
  60. using System;
 using System.Threading.Tasks;
 using System.Threading;
 
 class async_await
 {


    static void Main(string[] args)
 {
 var test = new AsyncTest();
 test.Test1();
 Console.ReadKey();
 }
 }
 
 class AsyncTest
 {
 //εϨουΛੜ੒͠ɺܧଓλεΫΛߦΘͤΔ
 //ॲཧB΋ॲཧAͱಉ͡εϨουͰߦΘΕ͍ͯΔ͜ͱʹ஫ҙ
 public async void Test1()
 {
 await Task.Run(() =>
 {
 //࣌ؒͷ͔͔ΔॲཧΛͯ͠΋awaitʹΑͬͯॲཧB͸࣮ߦ͞Εͳ͍
 Thread.Sleep(100);
 Console.WriteLine("Test1ॲཧA");
 });
 Console.WriteLine("Test1ॲཧB");//ܧଓॲཧ
 }
 }
  61. using System;
 using System.Threading.Tasks;
 using System.Threading;
 
 
 class async_await


    {
 static void Main(string[] args)
 {
 var test = new AsyncTest();
 //√3Λදࣔ͢Δɻ
 //ResultϓϩύςΟʹΑΓॲཧऴྃ࣌·ͰϝΠϯεϨου΋ऴྃ͠ͳ͍
 Console.WriteLine(test.SumTwoAndSqrt(1.0, 2.0).Result);
 }
 }
 
 class AsyncTest
 {
 //2ͭͷҾ਺Λ଍ͯ͠ฏํࠜΛऔΔϝιουɻ
 //Sqrt͕return͢Δͷ͸doubleܕ͔ͩɺasyncम০ʹΑΓTask<double>ܕʹม׵͞ΕΔ
 public async Task<double> SumTwoAndSqrt(double num1, double num2)
 {
 //ຊདྷͳΒRun͸Task<double>Λฦ͕͢
 //awaitम০ʹΑΓ݁Ռͷdoubleܕม਺ͷΈ͕நग़͞Εͨ
 double result = await Task.Run(() => num1 + num2);
 return Math.Sqrt(result);//લͷॲཧ݁ՌresultΛ༰қʹ࢖͑Δ
 }
 }

  62. ڞ௨ͷࢿݯΛར༻͢Δ

  63. lockεςʔτϝϯτ MonitorΫϥεͷ؆қߏจ ϩοΫ͞ΕͨίʔυΛผͷεϨου͕࢖͓͏ͱ͢ΔͱɺΦϒδΣ Ϋτ͕ղ์͞ΕΔ·Ͱ଴ػ (ϒϩοΫ) ͢Δ lock͢Δ΂͖ΦϒδΣΫτΛlock͠ͳ͍Ͱ࢖༻͢ΔϝιουͳͲ ͕ଘࡏͨ͠৔߹ɺlockΛ࢖͏ίʔυࣗ਎΋҆શͱݴ͑ͳ͘ͳΔ ڞ௨ʹѻ͏lock͕ඞཁͳ΋ͷΛࢀর͢Δ৔߹ɺࢀর͢ΔશͯͷՕ ॴͰlock͢΂͖Ͱ͋Δ

  64. using System;
 using System.Threading.Tasks;
 using System.Threading;
 
 
 class MainClass


    {
 public static void Main(string[] args)
 {
 var t1 = Task.Run(() =>
 {
 Console.WriteLine(ReadTargetLocked());
 });
 var t2 = Task.Run(() =>
 {
 IncrementLocked();
 });
 Task.WaitAll(t1, t2);
 }
 
 static int target = 1110;
 static object lockObj = new object();
 
 static int ReadTargetLocked()
 {
 lock (lockObj)
 {
 Thread.Sleep(1000);
 return target;
 }
 }
 
 static void IncrementLocked()
 {
 lock (lockObj)
 {
 ++target;
 }
 }
 } ΦϒδΣΫτ͕ϩοΫ͞Ε͍ͯΔͷͰ ଞͷεϨου͕ͦͷΦϒδΣΫτΛ ࢀর͠Α͏ͱ͢Δͱ ΦϒδΣΫτ͕ղ์͞ΕΔ·Ͱ଴ػ (ϒϩοΫ) ͞ΕΔ ௨ৗ͸lock༻ʹΦϒδΣΫτ Λ1ͭ༻ҙ͓ͯ͘͠ ڞ༗͍ͨ͠ΦϒδΣΫτࣗ਎Ͱ΋͍͍͕ ϩοΫม਺Λpublicͱͯ͠ѻ͍͍ͨ৔߹͸ σουϩοΫͷݪҼʹͳΓ͔Ͷͳ͍ ϩοΫ༻ม਺Λ༻ҙ͢Ε͹publicͰ΋ແ໰୊
  65. TPL

  66. TPLͷجຊ Task Parallel Library λεΫΛΑΓ؆୯ʹ࢖༻Ͱ͖Δ৔߹ ͕͋Δ

  67. using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;


    using System.Threading;
 
 class TPL
 {
 static void Main(string[] args)
 {
 var act = new Action[10];
 for (int i = 0; i < 10; ++i)
 act[i] = new Action(Show);
 //Invoke͸શͯ׬ྃ͢Δ·ͰϝΠϯεϨου͕ऴྃ͠ͳ͍ 
 Parallel.Invoke(act);
 Console.WriteLine("exit...");
 }
 
 static void Show()
 {
 Console.WriteLine("Hello.");
 }
 }
  68. using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;


    using System.Threading;
 
 class TPL
 {
 static void Main(string[] args)
 {
 //For͸શͯ׬ྃ͢Δ·ͰϝΠϯεϨου͕ऴྃ͠ͳ͍
 Parallel.For(0, 10, n =>
 {
 Thread.Sleep(100);
 Console.WriteLine(n);
 });
 
 Console.WriteLine("exit...");
 }
 }
  69. using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;


    using System.Threading;
 
 class TPL
 {
 static void Main(string[] args)
 {
 //ForAll͸શ࣮ͯߦ͞ΕΔ·ͰϝΠϯεϨου͕ऴྃ͠ͳ͍
 Enumerable.Range(0, 10).AsParallel().ForAll(n =>
 {
 Thread.Sleep(100);
 Console.WriteLine(n);
 });
 
 Console.WriteLine("exit...");
 }
 }