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

dartの排他制御おさらいしてみた

RyuNen344
February 22, 2022

 dartの排他制御おさらいしてみた

RyuNen344

February 22, 2022
Tweet

More Decks by RyuNen344

Other Decks in Technology

Transcript

  1. About Me • νʔϜϥϘॴଐ • (ࣗশ)ϚϧνϓϥοτϑΥʔϜܳਓ • KMM, Flutter etc...

    • oura ringΛฆࣦͨ͠γϣοΫͰ৸ࠐΜͩ(ઌि༵ۚ೔ʹ৽͍͠ͷಧ͍ ͨ)
  2. Concurrency of Flutter(Dart VM) • Dart͸Single ThreadɾEvent Loop(iOS, androidಉ͡) •

    iOS, androidΤϯδχΞ͕૝ఆ͢Δthread͸Isolateͱ͍͏୯ҐͰѻΘΕΔ • ϝϞϦͷڞ༗͸ߦ͑ͣɺϝοηʔδΛ༻͍ͯσʔλΛ΍ΓऔΓ͢Δ • https://dart.dev/guides/language/concurrency#how-isolates-work ʹ͸👇ͷه ࡌ • you don’t have to worry about mutexes or locks • ϝϞϦ͕ڞ༗͞Εͳ͍ͷͰallocͱgarbage collectʹmutex΍lock͕ඞཁͳ͍
  3. Basics of Isolate • جຊతʹ͸JavaͷϚϧνεϨουͷײ֮Ͱ࢖༻͢Ε͹Α͍͕͍͔ͭ͘஫ҙͨ͠΄͏͕Α ͍ • IsolateΛੜ੒͢Δࡍͷίετ͕ωΠςΟϒΑΓएׯߴ͍ • ϝϞϦ͕Isolate͝ͱʹ෼཭͞Ε͍ͯΔͷͰม਺ʹΞΫηε͢ΔܗͰॻ͍ͯ΋Main

    Isolateʹ͸൓ө͞Εͳ͍ • Isolate಺Ͱൃੜͨ͠error͸RemoteError or Exceptionʹϥοϓ͞ΕͨܗͰฦ٫͞ΕΔ • Isolate APIΛ௚઀࢖͏ΑΓFlutterͷcomputeΛ࢖༻͢Δͱॻ͖΍͍͢
  4. Cost of Isolate • mono͞Μͷmedium͕Θ͔Γ΍͍͢ͷͰׂѪ • https://medium.com/ fl utter-jp/isolate-a3f6eab488b5 •

    ωοτϫʔΫϨεϙϯε଴ͪ౳ͷCPUෛՙ͕௿͍΋ͷʹ͍ͭͯ͸Main IsolateͰ΍ͬͨ ΄͏͕Α͍ • CPUෛՙ͕ߴ͍΋ͷ(ܭࢉΛߦ͏ϧʔϓॲཧɺେ͖͍ϑΝΠϧͷγϦΞϥΠζ)͸Isolate Λ࢖ͬͯ͋͛Δͱྑͦ͞͏ • େ͖͍ϑΝΠϧͷγϦΞϥΠζͰ΋2018೥࣌఺ͰIsolateͷεΠονϯάίετͷ΄͏ ͕ߴ͍ͭͨ͘
  5. Access to variables in Isolate • ϝϞϦ͕Isolate͝ͱʹ෼཭͞Ε͍ͯΔͱ͍͏͜ͱ͸Isolate಺Ͱม਺Λ ߋ৽ͯ͠΋Main Isolate্Ͱ͸൓ө͞Εͳ͍ •

    👆Λߟ͑ΔͱIsolate͸εϨουͱೝࣝ͢ΔΑΓϓϩηεͱೝࣝͯ͋͠ ͛Δͱ͍͍ײ͡ • ഉଞ੍ޚ͍Βͳ͍ means Main IsolateͰॱ࣍Α͠ͳʹ΍ͬͯͶ😉ͱ͍ ͏͜ͱ w w w w w w w w
  6. Example of accessing to variables in Isolate • લϖʔδͷ࣮૷Ͱ͸ผεϨουͰτʔΫϯʹΞΫηεͯ͠͠·͍ͬͯ Δ

    • DartͰ͸ผIsolateͰݺͼग़͞ΕΔͱߋ৽ͨ͠τʔΫϯͷ஋MainIsolate Ͱ͸औಘͰ͖ͳ͍😩 • 👉ં֯τʔΫϯߋ৽ͨ͠ͷʹ࠶౓ϝιουΛୟ͍ͨͱ͖ʹ஋͕ߋ৽͞ Ε͍ͯͳ͍ɾɾɾͳΜͯ͜ͱʹɾɾɾɾ
  7. Example of accessing to variables in Isolate τʔΫϯಡΈग़͠ॲཧΛMain Isolate Ͱ࣮ࢪ͢ΔΑ͏ʹมߋ

    τʔΫϯࣗମ͸Ҿ਺ͰผIsolateͰݺ͹ ΕΔϝιουʹ౉͢ τʔΫϯॻ͖ग़͠ॲཧΛMain Isolate Ͱ࣮ࢪ͢ΔΑ͏ʹมߋ
  8. Error in Isolate(ara ara shi handling) • ϥοϓ͞Ε·ͬͯͯ͘πϥ͍ɾɾɾɾ👼 • Exception.messageͷจࣈྻ൑ఆͰΤϥʔϋϯυϦϯά͢Δ͜ͱ΋ෆ

    Մೳ͡Όͳ͍͕πϥ͍ɾɾɾɾ👼 • ϝϞϦόοϑΝΛߟྀͯ͠StringͷΈͰ΍ΓऔΓ͍ͯ͠Δͷ͔΋͠Ε ͳ͍͕ߥʑ͘͠ExceptionΛड͚औΔํ๏Λ໛ࡧͯ͠ΈΔ