SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
Blocks & GCD
• History	

• GCD	

• Queues	

• Blocks	

• Syntax and Examples	

• Demo
Grand Central Dispatch
History	

•  Historically, microprocessors gained speed by
running at faster and faster clock speeds; and
software become automatically faster.	

•  Processor clock speeds began to reach a limit
because power consumption and heat became
problematic, particularly for mobile systems.	

•  CPU vendors shifted their focus from
increasing clock speed to putting multiple
processor cores into a single CPU 	

•  so#ware	
  no	
  longer	
  automa/cally	
  becomes	
  
faster.	
  
History	

•  We required threads to make use of
multithreading, NSThread is not that simple to
use for each and every small data processing
task, need locks.	

•  NSOprationQueue was an alternative, but not
so lightweight and required some boilerplate
code	

•  Or use 	

o performSelectorInBackground:withObject:	

o 
performSelectorOnMainThread:withObject:waitUn
tilDone:
Solu/on	
  
•  The	
  dominant	
  model	
  for	
  concurrent	
  programming
—threads	
  and	
  locks—is	
  too	
  difficult	
  to	
  be	
  worth	
  
the	
  effort	
  for	
  most	
  applica/ons.	
  To	
  write	
  a	
  an	
  
efficient	
  applica/on	
  for	
  mul/-­‐	
  core	
  using	
  threads,	
  
you	
  need	
  to:	
  	
  
– Break	
  each	
  logical	
  task	
  down	
  to	
  a	
  single	
  thread	
  
Lock	
  data	
  that	
  is	
  in	
  danger	
  of	
  being	
  changed	
  by	
  two	
  
threads	
  at	
  once	
  
Build	
  a	
  thread	
  manager	
  to	
  run	
  only	
  as	
  many	
  threads	
  as	
  
there	
  are	
  available	
  cores	
  Hope	
  that	
  no	
  other	
  
applica/ons	
  running	
  on	
  the	
  system	
  are	
  using	
  the	
  
processor	
  cores	
  	
  
How?	
  
•  GCD	
  shi#s	
  the	
  responsibility	
  for	
  managing	
  
threads	
  and	
  their	
  execu/on	
  from	
  applica/ons	
  to	
  
the	
  opera/ng	
  system.	
  	
  
•  Units	
  of	
  work	
  are	
  described	
  as	
  blocks	
  in	
  your	
  
code,	
  while	
  queues	
  are	
  used	
  to	
  organize	
  blocks	
  
based	
  on	
  how	
  you	
  believe	
  they	
  need	
  to	
  be	
  
executed.	
  	
  
•  GCD	
  has	
  a	
  mul/core	
  execu/on	
  engine	
  that	
  reads	
  
the	
  queues	
  created	
  by	
  each	
  applica/on	
  and	
  
assigns	
  work	
  from	
  the	
  queues	
  to	
  the	
  threads	
  it	
  is	
  
managing.	
  	
  
Hooray!	

•  Apple introduced Grand Central Dispatch and
Blocks for SnowLeopard; and decided to
remove support for PPC.	

•  Apple came up with multicore HandHelds,
soon after blocks and GCD were announced
for iOS
Programming	
  Model	
  
•  Blocks	
  are	
  used	
  as	
  a	
  Unit	
  of	
  Work	
  
•  Dispatch	
  Objects	
  -­‐	
  reference	
  counted,	
  uses	
  
dispatch_retain()	
  and	
  dispatch_release()	
  
•  Queues	
  (four	
  system	
  defined)	
  are	
  used	
  to	
  
execute	
  Blocks	
  
– Serial	
  /	
  Concurrent	
  	
  
•  Event	
  Sources	
  –	
  associate	
  blocks/queues	
  to	
  
asynchronous	
  event	
  source	
  e.g.	
  /mer,	
  socket	
  
•  A	
  thread-­‐pool	
  of	
  max	
  512	
  threads	
  can	
  be	
  
maintained,	
  old	
  threads	
  are	
  reused	
  
Queues
Queues	
  
•  Most	
  of	
  the	
  intelligence	
  behind	
  Grand	
  Central	
  
Dispatch	
  is	
  provided	
  by	
  queues.	
  	
  
– Global	
  Queues	
  
– Private	
  Queues	
  
– Main	
  Queue	
  
•  A	
  queue	
  can	
  execute	
  opera/on	
  in	
  Sync	
  or	
  
Async	
  
Queues	
  
dispatch_queue_t	
  dispatch_get_global_queue(	
  
	
  	
  	
  long	
  priority,	
  
	
  	
  	
  unsigned	
  long	
  flags);	
  
DISPATCH_QUEUE_PRIORITY_HIGH	
  
DISPATCH_QUEUE_PRIORITY_DEFAULT	
  
DISPATCH_QUEUE_PRIORITY_LOW	
  
dispatch_queue_t	
  dispatch_queue_create(	
  
	
  	
  	
  const	
  char	
  *label	
  
	
  	
  	
  dispatch_queue_afr_t	
  afr);	
  
dispatch_queue_t	
  dispatch_get_main_queue(void);	
  
Using	
  Queues	
  
Using	
  Queues	
  
Using	
  Queues	
  
•  the	
  block	
  that's	
  submifed	
  with	
  the	
  barrier	
  func/on	
  doesn't	
  run	
  
concurrently	
  with	
  other	
  work	
  on	
  that	
  queue	
  
•  pointless	
  on	
  a	
  serial	
  queue	
  
•  non-­‐func/onal	
  when	
  used	
  on	
  the	
  global	
  queues	
  
Blocks
Blocks:What is it?	

•  Blocks are a nonstandard extension added by
Apple Inc. to the C, C++, and Objective-C
programming languages that uses a lambda
expression-like syntax to create closures
within these languages. Blocks are supported
for programs developed for Mac OS X 10.6+
and iOS 4.0+.	

– wikipedia
What is it?	

•  A	
  block	
  is	
  an	
  anonymous	
  inline	
  collec/on	
  of	
  
code	
  that:	
  
– Has	
  a	
  typed	
  argument	
  list	
  just	
  like	
  a	
  func/on	
  
What is it?	

•  A	
  block	
  is	
  an	
  anonymous	
  inline	
  collec/on	
  of	
  
code	
  that:	
  
– Has	
  an	
  inferred	
  or	
  declared	
  return	
  type	
  
What is it?	

•  A	
  block	
  is	
  an	
  anonymous	
  inline	
  collec/on	
  of	
  
code	
  that:	
  
– Can	
  capture	
  state	
  from	
  the	
  lexical	
  scope	
  within	
  
which	
  it	
  is	
  defined
What is it?	

•  A	
  block	
  is	
  an	
  anonymous	
  inline	
  collec/on	
  of	
  
code	
  that:	
  
– Can	
  op/onally	
  modify	
  the	
  state	
  of	
  the	
  lexical	
  scope	
  
What is it?	

•  A	
  block	
  is	
  an	
  anonymous	
  inline	
  collec/on	
  of	
  
code	
  that:	
  
– Can	
  share	
  the	
  poten/al	
  for	
  modifica/on	
  with	
  other	
  
blocks	
  defined	
  within	
  the	
  same	
  lexical	
  scope	
  
• Multiple blocks in the same lexical score shares the
same instance.	

• If a variable is __block is is passed as reference and
hence can be accessed by multiple blocks
What is it?	

•  A	
  block	
  is	
  an	
  anonymous	
  inline	
  collec/on	
  of	
  
code	
  that:	
  
– Can	
  con/nue	
  to	
  share	
  and	
  modify	
  state	
  defined	
  
within	
  the	
  lexical	
  scope	
  (the	
  stack	
  frame)	
  a#er	
  the	
  
lexical	
  scope	
  (the	
  stack	
  frame)	
  has	
  been	
  destroyed	
  
• Each	
  __block	
  has	
  a	
  __strong	
  reference	
  hence	
  even	
  if	
  
current	
  stack	
  frame	
  is	
  destroyed	
  it	
  can	
  work	
  on	
  that	
  
variable.	
  
• The	
  compiler	
  and	
  run/me	
  arrange	
  that	
  all	
  variables	
  
referenced	
  from	
  the	
  block	
  are	
  preserved	
  for	
  the	
  life	
  of	
  
all	
  copies	
  of	
  the	
  block	
  
What is it?	

•  You	
  can	
  copy	
  a	
  block	
  and	
  even	
  pass	
  it	
  to	
  other	
  
threads	
  for	
  deferred	
  execu/on	
  (or,	
  within	
  its	
  
own	
  thread,	
  to	
  a	
  runloop).	
  (use	
  Block_copy	
  
instead)	
  
Blocks	
  Usage	
  
•  Used	
  for	
  lightweight	
  task,	
  Blocks	
  represent	
  
typically	
  small,	
  self-­‐contained	
  pieces	
  of	
  code.	
  	
  
•  They’re	
  par/cularly	
  useful	
  as	
  a	
  means	
  of	
  
encapsula/ng	
  units	
  of	
  work	
  that	
  may	
  be	
  
executed	
  concurrently,	
  or	
  over	
  items	
  in	
  a	
  
collec/on,	
  or	
  as	
  a	
  callback	
  when	
  another	
  
opera/on	
  has	
  finished.	
  
Small,	
  Self	
  contained	
  
Work	
  over	
  over	
  items	
  in	
  a	
  collec/on	
  
Callbacks	
  
•  Asynchronus	
  Network	
  tasks	
  
•  Snippet	
  from	
  MKNetworkEngine.m	
  by	
  @mugunthkumar	
  
Usage	
  in	
  cocoa	
  
•  Asynchronus	
  UI	
  tasks	
  
Under	
  the	
  Hood	
  
•  Extracted	
  from	
  "Pro	
  Mul.threading	
  and	
  Memory	
  Management	
  for	
  iOS	
  and	
  OS	
  X	
  with	
  ARC,	
  
Grand	
  Central	
  Dispatch,	
  and	
  Blocks"	
  by	
  Kazuki	
  Sakamoto	
  and	
  Tomohiko	
  Furumoto	
  
void	
  (^blk)(void)	
  =	
  ^{prinq("Blockn");};	
  	
  
is	
  translated	
  to	
  	
  
sta/c	
  void	
  __main_block_func_0(struct	
  __main_block_impl_0	
  
*__cself)	
  {prinq("Blockn");	
  }	
  
by	
  compiler,	
  there	
  is	
  lot	
  of	
  other	
  boilerplate	
  
code	
  created	
  to	
  help	
  invoca/on	
  of	
  this	
  method	
  
and	
  other	
  blocks	
  opera/on	
  (sharing	
  a	
  variable,	
  
copy,	
  pass	
  by	
  reference)	
  
Dispatch	
  Source
Dispatch	
  Source	
  
•  dispatch	
  source	
  is	
  an	
  object	
  which	
  monitors	
  for	
  
one	
  of	
  following	
  event	
  :	
  
– Mach	
  port	
  send	
  right	
  state	
  changes.	
  
– Mach	
  port	
  receive	
  right	
  state	
  changes.	
  
– External	
  process	
  state	
  change.	
  
– File	
  descriptor	
  ready	
  for	
  read.	
  
– File	
  descriptor	
  ready	
  for	
  write.	
  
– Filesystem	
  node	
  event.	
  (kqueue)	
  
– POSIX	
  signal.	
  
– Custom	
  /mer.	
  
– Custom	
  event.	
  
Dispatch	
  Source	
  example	
  
Thank	
  You!	
  
•  Sources:	
  
– Apple	
  Documenta/on	
  
– libdispatch	
  Wiki	
  
– CGD	
  Technology	
  Brief	
  
Prepared	
  for	
  PuneCocoa	
  
By	
  Prashant	
  Rane	
  
	
  (@the69geeks)	
  

Contenu connexe

Tendances

.NET Core, ASP.NET Core Course, Session 2
.NET Core, ASP.NET Core Course, Session 2.NET Core, ASP.NET Core Course, Session 2
.NET Core, ASP.NET Core Course, Session 2aminmesbahi
 
Functional approach to packet processing
Functional approach to packet processingFunctional approach to packet processing
Functional approach to packet processingNicola Bonelli
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3aminmesbahi
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java ConcurrencyBen Evans
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waitingRoman Elizarov
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...Sachintha Gunasena
 
Serialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsSerialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsAlex Tumanoff
 
Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# WayBishnu Rawal
 
Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)Ben Evans
 
Google appenginejava.ppt
Google appenginejava.pptGoogle appenginejava.ppt
Google appenginejava.pptYoung Alista
 
Motivation for multithreaded architectures
Motivation for multithreaded architecturesMotivation for multithreaded architectures
Motivation for multithreaded architecturesYoung Alista
 
Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)오석 한
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4aminmesbahi
 
PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 9th Italian Networking Workshop (Courmayeur)PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 9th Italian Networking Workshop (Courmayeur)Nicola Bonelli
 

Tendances (20)

.NET Core, ASP.NET Core Course, Session 2
.NET Core, ASP.NET Core Course, Session 2.NET Core, ASP.NET Core Course, Session 2
.NET Core, ASP.NET Core Course, Session 2
 
Functional approach to packet processing
Functional approach to packet processingFunctional approach to packet processing
Functional approach to packet processing
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
PF_DIRECT@TMA12
PF_DIRECT@TMA12PF_DIRECT@TMA12
PF_DIRECT@TMA12
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java Concurrency
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waiting
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
 
Serialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsSerialization and performance by Sergey Morenets
Serialization and performance by Sergey Morenets
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# Way
 
Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)Perl On The JVM (London.pm Talk 2009-04)
Perl On The JVM (London.pm Talk 2009-04)
 
Google appenginejava.ppt
Google appenginejava.pptGoogle appenginejava.ppt
Google appenginejava.ppt
 
Motivation for multithreaded architectures
Motivation for multithreaded architecturesMotivation for multithreaded architectures
Motivation for multithreaded architectures
 
Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
Thread
ThreadThread
Thread
 
PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 9th Italian Networking Workshop (Courmayeur)PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 9th Italian Networking Workshop (Courmayeur)
 
Java8 - Under the hood
Java8 - Under the hoodJava8 - Under the hood
Java8 - Under the hood
 
Lecture6
Lecture6Lecture6
Lecture6
 

En vedette

Political priocess among tribals
Political priocess among tribalsPolitical priocess among tribals
Political priocess among tribalsKurian Adhikarathil
 
Professional Persona
Professional PersonaProfessional Persona
Professional PersonaJoseph Bruss
 
Wk2 project storyboard
Wk2 project  storyboardWk2 project  storyboard
Wk2 project storyboardJoseph Bruss
 
Change The World
Change The WorldChange The World
Change The WorldRobert Root
 
Paranormal activity trailer analysis
Paranormal activity trailer analysisParanormal activity trailer analysis
Paranormal activity trailer analysisDean Tomlinson
 
Media man of steel trailer analysis.
Media man of steel trailer analysis.Media man of steel trailer analysis.
Media man of steel trailer analysis.Dean Tomlinson
 
Formation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à Mayotte
Formation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à MayotteFormation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à Mayotte
Formation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à MayotteRemy EXELMANS
 
Veille Stratégique - IFAG Réunion - novembre 2016
Veille Stratégique - IFAG Réunion - novembre 2016 Veille Stratégique - IFAG Réunion - novembre 2016
Veille Stratégique - IFAG Réunion - novembre 2016 Remy EXELMANS
 

En vedette (16)

Identifying genre
Identifying genreIdentifying genre
Identifying genre
 
Presentation
PresentationPresentation
Presentation
 
Political priocess among tribals
Political priocess among tribalsPolitical priocess among tribals
Political priocess among tribals
 
Presentation
PresentationPresentation
Presentation
 
Professional Persona
Professional PersonaProfessional Persona
Professional Persona
 
The muscular system
The muscular systemThe muscular system
The muscular system
 
Wk2 project storyboard
Wk2 project  storyboardWk2 project  storyboard
Wk2 project storyboard
 
Change The World
Change The WorldChange The World
Change The World
 
Paranormal activity trailer analysis
Paranormal activity trailer analysisParanormal activity trailer analysis
Paranormal activity trailer analysis
 
Media man of steel trailer analysis.
Media man of steel trailer analysis.Media man of steel trailer analysis.
Media man of steel trailer analysis.
 
The challenge of life
The challenge of life The challenge of life
The challenge of life
 
Question 4
Question 4Question 4
Question 4
 
Formation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à Mayotte
Formation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à MayotteFormation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à Mayotte
Formation Stratégie d'Entreprise - Accompagnement des dirigeants NTIC à Mayotte
 
Hawalah dan Ji'alah
Hawalah dan Ji'alahHawalah dan Ji'alah
Hawalah dan Ji'alah
 
Veille Stratégique - IFAG Réunion - novembre 2016
Veille Stratégique - IFAG Réunion - novembre 2016 Veille Stratégique - IFAG Réunion - novembre 2016
Veille Stratégique - IFAG Réunion - novembre 2016
 
Kepribadian dan Nilai
Kepribadian dan Nilai Kepribadian dan Nilai
Kepribadian dan Nilai
 

Similaire à gcdtmp

Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPrashant Rane
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
FRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxFRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxEhtesham46
 
Latest (storage IO) patterns for cloud-native applications
Latest (storage IO) patterns for cloud-native applications Latest (storage IO) patterns for cloud-native applications
Latest (storage IO) patterns for cloud-native applications OpenEBS
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Martijn Verburg
 
Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Mike Slinn
 
Concurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceConcurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceKaniska Mandal
 
Terraform training - Modules 🎒
Terraform training - Modules 🎒Terraform training - Modules 🎒
Terraform training - Modules 🎒StephaneBoghossian1
 
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceCOMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceAntonio García-Domínguez
 
6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptx6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptxSimRelokasi2
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at TwitterAlex Payne
 
OpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander Dibbo
OpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander DibboOpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander Dibbo
OpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander DibboOpenNebula Project
 

Similaire à gcdtmp (20)

Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
Freckle
FreckleFreckle
Freckle
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
FRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxFRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptx
 
Latest (storage IO) patterns for cloud-native applications
Latest (storage IO) patterns for cloud-native applications Latest (storage IO) patterns for cloud-native applications
Latest (storage IO) patterns for cloud-native applications
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
 
Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Composable Futures with Akka 2.0
Composable Futures with Akka 2.0
 
Concurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceConcurrency Learning From Jdk Source
Concurrency Learning From Jdk Source
 
Terraform training - Modules 🎒
Terraform training - Modules 🎒Terraform training - Modules 🎒
Terraform training - Modules 🎒
 
01 oracle architecture
01 oracle architecture01 oracle architecture
01 oracle architecture
 
Memory model
Memory modelMemory model
Memory model
 
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceCOMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
 
6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptx6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptx
 
Stackato v4
Stackato v4Stackato v4
Stackato v4
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
OpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander Dibbo
OpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander DibboOpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander Dibbo
OpenNebulaConf2015 1.07 Cloud for Scientific Computing @ STFC - Alexander Dibbo
 

Dernier

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 

Dernier (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 

gcdtmp

  • 4. History •  Historically, microprocessors gained speed by running at faster and faster clock speeds; and software become automatically faster. •  Processor clock speeds began to reach a limit because power consumption and heat became problematic, particularly for mobile systems. •  CPU vendors shifted their focus from increasing clock speed to putting multiple processor cores into a single CPU •  so#ware  no  longer  automa/cally  becomes   faster.  
  • 5. History •  We required threads to make use of multithreading, NSThread is not that simple to use for each and every small data processing task, need locks. •  NSOprationQueue was an alternative, but not so lightweight and required some boilerplate code •  Or use o performSelectorInBackground:withObject: o  performSelectorOnMainThread:withObject:waitUn tilDone:
  • 6. Solu/on   •  The  dominant  model  for  concurrent  programming —threads  and  locks—is  too  difficult  to  be  worth   the  effort  for  most  applica/ons.  To  write  a  an   efficient  applica/on  for  mul/-­‐  core  using  threads,   you  need  to:     – Break  each  logical  task  down  to  a  single  thread   Lock  data  that  is  in  danger  of  being  changed  by  two   threads  at  once   Build  a  thread  manager  to  run  only  as  many  threads  as   there  are  available  cores  Hope  that  no  other   applica/ons  running  on  the  system  are  using  the   processor  cores    
  • 7. How?   •  GCD  shi#s  the  responsibility  for  managing   threads  and  their  execu/on  from  applica/ons  to   the  opera/ng  system.     •  Units  of  work  are  described  as  blocks  in  your   code,  while  queues  are  used  to  organize  blocks   based  on  how  you  believe  they  need  to  be   executed.     •  GCD  has  a  mul/core  execu/on  engine  that  reads   the  queues  created  by  each  applica/on  and   assigns  work  from  the  queues  to  the  threads  it  is   managing.    
  • 8. Hooray! •  Apple introduced Grand Central Dispatch and Blocks for SnowLeopard; and decided to remove support for PPC. •  Apple came up with multicore HandHelds, soon after blocks and GCD were announced for iOS
  • 9. Programming  Model   •  Blocks  are  used  as  a  Unit  of  Work   •  Dispatch  Objects  -­‐  reference  counted,  uses   dispatch_retain()  and  dispatch_release()   •  Queues  (four  system  defined)  are  used  to   execute  Blocks   – Serial  /  Concurrent     •  Event  Sources  –  associate  blocks/queues  to   asynchronous  event  source  e.g.  /mer,  socket   •  A  thread-­‐pool  of  max  512  threads  can  be   maintained,  old  threads  are  reused  
  • 11. Queues   •  Most  of  the  intelligence  behind  Grand  Central   Dispatch  is  provided  by  queues.     – Global  Queues   – Private  Queues   – Main  Queue   •  A  queue  can  execute  opera/on  in  Sync  or   Async  
  • 12. Queues   dispatch_queue_t  dispatch_get_global_queue(        long  priority,        unsigned  long  flags);   DISPATCH_QUEUE_PRIORITY_HIGH   DISPATCH_QUEUE_PRIORITY_DEFAULT   DISPATCH_QUEUE_PRIORITY_LOW   dispatch_queue_t  dispatch_queue_create(        const  char  *label        dispatch_queue_afr_t  afr);   dispatch_queue_t  dispatch_get_main_queue(void);  
  • 15. Using  Queues   •  the  block  that's  submifed  with  the  barrier  func/on  doesn't  run   concurrently  with  other  work  on  that  queue   •  pointless  on  a  serial  queue   •  non-­‐func/onal  when  used  on  the  global  queues  
  • 17. Blocks:What is it? •  Blocks are a nonstandard extension added by Apple Inc. to the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+. – wikipedia
  • 18. What is it? •  A  block  is  an  anonymous  inline  collec/on  of   code  that:   – Has  a  typed  argument  list  just  like  a  func/on  
  • 19. What is it? •  A  block  is  an  anonymous  inline  collec/on  of   code  that:   – Has  an  inferred  or  declared  return  type  
  • 20. What is it? •  A  block  is  an  anonymous  inline  collec/on  of   code  that:   – Can  capture  state  from  the  lexical  scope  within   which  it  is  defined
  • 21. What is it? •  A  block  is  an  anonymous  inline  collec/on  of   code  that:   – Can  op/onally  modify  the  state  of  the  lexical  scope  
  • 22. What is it? •  A  block  is  an  anonymous  inline  collec/on  of   code  that:   – Can  share  the  poten/al  for  modifica/on  with  other   blocks  defined  within  the  same  lexical  scope   • Multiple blocks in the same lexical score shares the same instance. • If a variable is __block is is passed as reference and hence can be accessed by multiple blocks
  • 23. What is it? •  A  block  is  an  anonymous  inline  collec/on  of   code  that:   – Can  con/nue  to  share  and  modify  state  defined   within  the  lexical  scope  (the  stack  frame)  a#er  the   lexical  scope  (the  stack  frame)  has  been  destroyed   • Each  __block  has  a  __strong  reference  hence  even  if   current  stack  frame  is  destroyed  it  can  work  on  that   variable.   • The  compiler  and  run/me  arrange  that  all  variables   referenced  from  the  block  are  preserved  for  the  life  of   all  copies  of  the  block  
  • 24. What is it? •  You  can  copy  a  block  and  even  pass  it  to  other   threads  for  deferred  execu/on  (or,  within  its   own  thread,  to  a  runloop).  (use  Block_copy   instead)  
  • 25. Blocks  Usage   •  Used  for  lightweight  task,  Blocks  represent   typically  small,  self-­‐contained  pieces  of  code.     •  They’re  par/cularly  useful  as  a  means  of   encapsula/ng  units  of  work  that  may  be   executed  concurrently,  or  over  items  in  a   collec/on,  or  as  a  callback  when  another   opera/on  has  finished.  
  • 27. Work  over  over  items  in  a  collec/on  
  • 28. Callbacks   •  Asynchronus  Network  tasks   •  Snippet  from  MKNetworkEngine.m  by  @mugunthkumar  
  • 29. Usage  in  cocoa   •  Asynchronus  UI  tasks  
  • 30. Under  the  Hood   •  Extracted  from  "Pro  Mul.threading  and  Memory  Management  for  iOS  and  OS  X  with  ARC,   Grand  Central  Dispatch,  and  Blocks"  by  Kazuki  Sakamoto  and  Tomohiko  Furumoto   void  (^blk)(void)  =  ^{prinq("Blockn");};     is  translated  to     sta/c  void  __main_block_func_0(struct  __main_block_impl_0   *__cself)  {prinq("Blockn");  }   by  compiler,  there  is  lot  of  other  boilerplate   code  created  to  help  invoca/on  of  this  method   and  other  blocks  opera/on  (sharing  a  variable,   copy,  pass  by  reference)  
  • 32. Dispatch  Source   •  dispatch  source  is  an  object  which  monitors  for   one  of  following  event  :   – Mach  port  send  right  state  changes.   – Mach  port  receive  right  state  changes.   – External  process  state  change.   – File  descriptor  ready  for  read.   – File  descriptor  ready  for  write.   – Filesystem  node  event.  (kqueue)   – POSIX  signal.   – Custom  /mer.   – Custom  event.  
  • 34. Thank  You!   •  Sources:   – Apple  Documenta/on   – libdispatch  Wiki   – CGD  Technology  Brief   Prepared  for  PuneCocoa   By  Prashant  Rane    (@the69geeks)