<s>
Concurrent	B-Language
Haskell	I-Language
extends	O
Haskell	B-Language
98	I-Language
with	O
explicit	O
concurrency	B-Operating_System
.	O
</s>
<s>
The	O
ability	O
to	O
spawn	O
a	O
concurrent	O
thread	B-Operating_System
via	O
the	O
forkIO	O
primitive	O
.	O
</s>
<s>
Built	O
atop	O
this	O
is	O
a	O
collection	O
of	O
useful	O
concurrency	B-Operating_System
and	O
synchronisation	O
abstractions	O
such	O
as	O
unbounded	O
channels	O
,	O
semaphores	B-Operating_System
and	O
sample	O
variables	O
.	O
</s>
<s>
Haskell	B-Language
threads	B-Operating_System
have	O
very	O
low	O
overhead	O
:	O
creation	O
,	O
context-switching	O
and	O
scheduling	O
are	O
all	O
internal	O
to	O
the	O
Haskell	B-Language
runtime	O
.	O
</s>
<s>
These	O
Haskell-level	O
threads	B-Operating_System
are	O
mapped	O
onto	O
a	O
configurable	O
number	O
of	O
OS-level	O
threads	B-Operating_System
,	O
usually	O
one	O
per	O
processor	O
core	O
.	O
</s>
<s>
The	O
software	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
(	O
STM	O
)	O
extension	O
to	O
GHC	B-Application
reuses	O
the	O
process	O
forking	O
primitives	O
of	O
Concurrent	B-Language
Haskell	I-Language
.	O
</s>
<s>
introduces	O
the	O
retry	O
and	O
orElse	O
primitives	O
,	O
allowing	O
alternative	O
atomic	B-General_Concept
actions	I-General_Concept
to	O
be	O
composed	O
together	O
.	O
</s>
<s>
The	O
STM	O
monad	O
is	O
an	O
implementation	O
of	O
software	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
in	O
Haskell	B-Language
.	O
</s>
<s>
It	O
is	O
implemented	O
in	O
GHC	B-Application
,	O
and	O
allows	O
for	O
mutable	O
variables	O
to	O
be	O
modified	O
in	O
transactions	B-General_Concept
.	O
</s>
<s>
If	O
there	O
were	O
two	O
transfers	O
transferring	O
money	O
from	O
account	O
from	O
,	O
and	O
both	O
calls	O
to	O
transfer	O
ran	O
line	O
(	O
A	O
)	O
before	O
either	O
of	O
them	O
had	O
written	O
their	O
new	O
values	O
,	O
it	O
is	O
possible	O
that	O
money	O
would	O
be	O
put	O
into	O
the	O
other	O
two	O
accounts	O
,	O
with	O
only	O
one	O
of	O
the	O
amounts	O
being	O
transferred	O
being	O
removed	O
from	O
account	O
from	O
,	O
thus	O
creating	O
a	O
race	B-Operating_System
condition	I-Operating_System
.	O
</s>
<s>
In	O
Haskell	B-Language
,	O
locking	O
is	O
accomplished	O
with	O
MVars	O
:	O
</s>
<s>
a	O
race	B-Operating_System
condition	I-Operating_System
still	O
exists	O
:	O
the	O
first	O
account	O
may	O
be	O
debited	O
,	O
then	O
execution	O
of	O
the	O
thread	B-Operating_System
may	O
be	O
suspended	O
,	O
leaving	O
the	O
accounts	O
as	O
a	O
whole	O
in	O
an	O
inconsistent	O
state	O
.	O
</s>
<s>
To	O
avoid	O
this	O
,	O
one	O
can	O
use	O
the	O
STM	O
monad	O
,	O
which	O
allows	O
one	O
to	O
write	O
atomic	O
transactions	B-General_Concept
.	O
</s>
<s>
This	O
means	O
that	O
all	O
operations	O
inside	O
the	O
transaction	O
fully	O
complete	O
,	O
without	O
any	O
other	O
threads	B-Operating_System
modifying	O
the	O
variables	O
that	O
our	O
transaction	O
is	O
using	O
,	O
or	O
it	O
fails	O
,	O
and	O
the	O
state	O
is	O
rolled	O
back	O
to	O
where	O
it	O
was	O
before	O
the	O
transaction	O
was	O
begun	O
.	O
</s>
<s>
In	O
short	O
,	O
atomic	O
transactions	B-General_Concept
either	O
complete	O
fully	O
,	O
or	O
it	O
is	O
as	O
if	O
they	O
were	O
never	O
run	O
at	O
all	O
.	O
</s>
<s>
The	O
return	O
types	O
of	O
STM	O
(	O
)	O
may	O
be	O
taken	O
to	O
indicate	O
that	O
we	O
are	O
composing	O
scripts	O
for	O
transactions	B-General_Concept
.	O
</s>
<s>
The	O
above	O
implementation	O
will	O
make	O
sure	O
that	O
no	O
other	O
transactions	B-General_Concept
interfere	O
with	O
the	O
variables	O
it	O
is	O
using	O
(	O
from	O
and	O
to	O
)	O
while	O
it	O
is	O
executing	O
,	O
allowing	O
the	O
developer	O
to	O
be	O
sure	O
that	O
race	B-Operating_System
conditions	I-Operating_System
like	O
that	O
above	O
are	O
not	O
encountered	O
.	O
</s>
<s>
More	O
improvements	O
can	O
be	O
made	O
to	O
make	O
sure	O
that	O
some	O
other	O
"	O
business	B-Architecture
logic	I-Architecture
"	O
is	O
maintained	O
in	O
the	O
system	O
,	O
i.e.	O
</s>
