<s>
Grand	B-Operating_System
Central	I-Operating_System
Dispatch	I-Operating_System
(	O
GCD	O
or	O
libdispatch	B-Operating_System
)	O
,	O
is	O
a	O
technology	O
developed	O
by	O
Apple	O
Inc	O
.	O
to	O
optimize	O
application	O
support	O
for	O
systems	O
with	O
multi-core	B-Architecture
processors	I-Architecture
and	O
other	O
symmetric	B-Operating_System
multiprocessing	I-Operating_System
systems	O
.	O
</s>
<s>
It	O
is	O
an	O
implementation	O
of	O
task	B-Operating_System
parallelism	I-Operating_System
based	O
on	O
the	O
thread	B-Operating_System
pool	I-Operating_System
pattern	I-Operating_System
.	O
</s>
<s>
The	O
fundamental	O
idea	O
is	O
to	O
move	O
the	O
management	O
of	O
the	O
thread	B-Operating_System
pool	I-Operating_System
out	O
of	O
the	O
hands	O
of	O
the	O
developer	O
,	O
and	O
closer	O
to	O
the	O
operating	O
system	O
.	O
</s>
<s>
GCD	O
was	O
first	O
released	O
with	O
Mac	B-Device
OS	I-Device
X	I-Device
10.6	I-Device
,	O
and	O
is	O
also	O
available	O
with	O
iOS	B-Operating_System
4	O
and	O
above	O
.	O
</s>
<s>
The	O
name	O
"	O
Grand	B-Operating_System
Central	I-Operating_System
Dispatch	I-Operating_System
"	O
is	O
a	O
reference	O
to	O
Grand	B-Operating_System
Central	I-Operating_System
Terminal	O
.	O
</s>
<s>
The	O
source	O
code	O
for	O
the	O
library	O
that	O
provides	O
the	O
implementation	O
of	O
GCD	O
's	O
services	O
,	O
libdispatch	B-Operating_System
,	O
was	O
released	O
by	O
Apple	O
under	O
the	O
Apache	B-Application
License	I-Application
on	O
September	O
10	O
,	O
2009	O
.	O
</s>
<s>
It	O
has	O
been	O
ported	O
to	O
FreeBSD	B-Operating_System
8.1	O
+	O
,	O
MidnightBSD	B-Application
0.3	O
+	O
,	O
Linux	O
,	O
and	O
Solaris	O
.	O
</s>
<s>
Attempts	O
in	O
2011	O
to	O
make	O
libdispatch	B-Operating_System
work	O
on	O
Windows	O
were	O
not	O
merged	O
into	O
upstream	O
.	O
</s>
<s>
Apple	O
has	O
its	O
own	O
port	O
of	O
libdispatch.dll	O
for	O
Windows	O
shipped	O
with	O
Safari	O
and	O
iTunes	O
,	O
but	O
no	O
SDK	B-Application
is	O
provided	O
.	O
</s>
<s>
Since	O
around	O
2017	O
,	O
the	O
original	O
libdispatch	B-Operating_System
repository	O
hosted	O
by	O
Nick	O
Hutchinson	O
was	O
deprecated	O
in	O
favor	O
of	O
a	O
version	O
that	O
is	O
part	O
of	O
the	O
Swift	B-Application
core	O
library	O
created	O
in	O
June	O
2016	O
.	O
</s>
<s>
A	O
task	B-General_Concept
can	O
be	O
expressed	O
either	O
as	O
a	O
function	O
or	O
as	O
a	O
"	O
block.	O
"	O
</s>
<s>
Blocks	O
are	O
an	O
extension	O
to	O
the	O
syntax	O
of	O
C	B-Language
,	O
C++	B-Language
,	O
and	O
Objective-C	B-Language
programming	I-Language
languages	I-Language
that	O
encapsulate	O
code	O
and	O
data	O
into	O
a	O
single	O
object	O
in	O
a	O
way	O
similar	O
to	O
a	O
closure	B-Language
.	O
</s>
<s>
Grand	B-Operating_System
Central	I-Operating_System
Dispatch	I-Operating_System
still	O
uses	O
threads	O
at	O
the	O
low	O
level	O
but	O
abstracts	O
them	O
away	O
from	O
the	O
programmer	O
,	O
who	O
will	O
not	O
need	O
to	O
be	O
concerned	O
with	O
as	O
many	O
details	O
.	O
</s>
<s>
A	O
task	B-General_Concept
in	O
Grand	B-Operating_System
Central	I-Operating_System
Dispatch	I-Operating_System
can	O
be	O
used	O
either	O
to	O
create	O
a	O
work	O
item	O
that	O
is	O
placed	O
in	O
a	O
queue	O
or	O
assign	O
it	O
to	O
an	O
event	O
source	O
.	O
</s>
<s>
If	O
a	O
task	B-General_Concept
is	O
assigned	O
to	O
an	O
event	O
source	O
,	O
then	O
a	O
work	O
unit	O
is	O
made	O
from	O
the	O
block	B-General_Concept
or	O
function	O
when	O
the	O
event	O
triggers	O
,	O
and	O
the	O
work	O
unit	O
is	O
placed	O
in	O
an	O
appropriate	O
queue	O
.	O
</s>
<s>
Because	O
a	O
serial	O
queue	O
can	O
only	O
run	O
one	O
task	B-General_Concept
at	O
a	O
time	O
,	O
each	O
task	B-General_Concept
submitted	O
to	O
the	O
queue	O
is	O
critical	O
with	O
regard	O
to	O
the	O
other	O
tasks	O
on	O
the	O
queue	O
,	O
and	O
thus	O
a	O
serial	O
queue	O
can	O
be	O
used	O
instead	O
of	O
a	O
lock	B-Operating_System
on	O
a	O
contended	O
resource	O
.	O
</s>
<s>
Dispatch	O
Sources	O
are	O
objects	O
that	O
allow	O
the	O
client	O
to	O
register	O
blocks	O
or	O
functions	O
to	O
execute	O
asynchronously	O
upon	O
system	O
events	O
,	O
such	O
as	O
a	O
socket	B-Protocol
or	O
file	B-Application
descriptor	I-Application
being	O
ready	O
for	O
reading	O
or	O
writing	O
,	O
or	O
a	O
POSIX	B-Operating_System
signal	I-Operating_System
.	O
</s>
<s>
Libdispatch	B-Operating_System
comes	O
with	O
its	O
own	O
object	O
model	O
,	O
OS	O
Object	O
,	O
that	O
is	O
partially	O
compatible	O
with	O
the	O
Objective-C	B-Language
model	O
.	O
</s>
<s>
As	O
a	O
result	O
,	O
its	O
objects	O
can	O
be	O
bridged	O
toll-free	O
to	O
ObjC	B-Language
objects	O
.	O
</s>
<s>
Two	O
examples	O
that	O
demonstrate	O
the	O
use	O
of	O
Grand	B-Operating_System
Central	I-Operating_System
Dispatch	I-Operating_System
can	O
be	O
found	O
in	O
John	O
Siracusa	O
's	O
Ars	O
Technica	O
Snow	B-Device
Leopard	I-Device
review	O
.	O
</s>
<s>
If	O
it	O
takes	O
long	O
enough	O
,	O
the	O
user	O
will	O
notice	O
,	O
and	O
the	O
application	O
may	O
even	O
"	O
beachball	B-Operating_System
"	O
.	O
</s>
<s>
Here	O
,	O
the	O
call	O
to	O
[	O
myDoc	O
analyze ]	O
is	O
placed	O
inside	O
a	O
Block	B-General_Concept
,	O
which	O
is	O
then	O
placed	O
on	O
one	O
of	O
the	O
global	O
concurrent	O
queues	O
.	O
</s>
<s>
After	O
it	O
has	O
finished	O
running	O
[	O
myDoc	O
analyze ]	O
,	O
a	O
new	O
block	B-General_Concept
is	O
placed	O
on	O
the	O
main	O
queue	O
(	O
on	O
which	O
the	O
main	O
thread	O
of	O
the	O
application	O
runs	O
)	O
,	O
which	O
updates	O
the	O
GUI	B-Application
(	O
This	O
is	O
necessary	O
because	O
the	O
GUI	B-Application
can	O
only	O
be	O
updated	O
by	O
the	O
main	O
thread	O
)	O
.	O
</s>
<s>
Here	O
,	O
dispatch_apply	O
runs	O
the	O
block	B-General_Concept
passed	O
to	O
it	O
,	O
count	O
times	O
,	O
placing	O
each	O
invocation	O
on	O
a	O
global	O
queue	O
,	O
and	O
passing	O
each	O
block	B-General_Concept
invocation	O
a	O
different	O
number	O
from	O
0	O
to	O
count-1	O
.	O
</s>
<s>
Care	O
must	O
be	O
taken	O
to	O
avoid	O
a	O
dispatched	O
block	B-General_Concept
on	O
a	O
queue	O
synchronously	O
placing	O
another	O
block	B-General_Concept
on	O
the	O
same	O
queue	O
as	O
this	O
is	O
guaranteed	O
to	O
deadlock	O
.	O
</s>
<s>
GCD	O
is	O
used	O
throughout	O
macOS	B-Operating_System
(	O
beginning	O
with	O
10.6	B-Device
Snow	B-Device
Leopard	I-Device
)	O
,	O
and	O
Apple	O
has	O
encouraged	O
its	O
adoption	O
by	O
macOS	B-Operating_System
application	O
developers	O
.	O
</s>
<s>
FreeBSD	B-Operating_System
developer	O
Robert	O
Watson	O
announced	O
the	O
first	O
adaptation	O
of	O
a	O
major	O
open	O
source	O
application	O
,	O
the	O
Apache	B-Application
HTTP	I-Application
Server	I-Application
,	O
to	O
use	O
GCD	O
via	O
the	O
Apache	B-Application
GCD	O
MPM	O
(	O
Multi-Processing	O
Module	O
)	O
on	O
May	O
11	O
,	O
2010	O
,	O
in	O
order	O
to	O
illustrate	O
the	O
programming	O
model	O
and	O
how	O
to	O
integrate	O
GCD	O
into	O
existing	O
,	O
large-scale	O
multi-threaded	O
,	O
applications	O
.	O
</s>
<s>
GCD	O
is	O
implemented	O
by	O
libdispatch	B-Operating_System
,	O
with	O
support	O
from	O
pthreads	O
non-POSIX	O
extensions	O
developed	O
by	O
Apple	O
.	O
</s>
<s>
Apple	O
has	O
changed	O
the	O
interface	O
since	O
its	O
inception	O
(	O
in	O
OS	B-Operating_System
X	I-Operating_System
10.5	O
)	O
through	O
the	O
official	O
launch	O
of	O
GCD	O
(	O
10.6	B-Device
)	O
,	O
Mountain	O
Lion	O
(	O
10.8	O
)	O
and	O
Mavericks	O
(	O
10.9	O
)	O
.	O
</s>
<s>
On	O
other	O
systems	O
,	O
libdispatch	B-Operating_System
implements	O
its	O
own	O
workqueue	O
using	O
the	O
system	O
's	O
own	O
event	O
facilities	O
(	O
epoll	O
,	O
kevent	O
,	O
or	O
Windows	O
NT	O
)	O
.	O
</s>
<s>
On	O
macOS	B-Operating_System
,	O
kevent	O
is	O
used	O
with	O
the	O
kernel	O
workqueue	O
.	O
</s>
