---
title: "Concurrent Programming in Erlang"  
description: "Erlang is a concurrent programming language means parallel activities can be programmed directly in Erlang."  
author: "Tarun Kumar"  
published: 2016-03-10  
updated: 2017-12-15  
canonical: https://www.mindstick.com/articles/12001/concurrent-programming-in-erlang  
category: "erlang"  
tags: ["erlang"]  
reading_time: 3 minutes  

---

# Concurrent Programming in Erlang

[Concurrency](https://www.mindstick.com/interview/23470/what-is-the-concurrency) is the backbone of Erlang. Concurrency means the different functions executes parallel without affecting each other. Each and every concurrent activity in Erlang is called a process, and the only way for a process to interact with each other is through message passing, in that way the data is sent from one process to another process. The idea behind the Erlang and its concurrency model is best described by Joe Armstrong:\

- The world is concurrent.\
- Things in the world don't [share data](https://www.mindstick.com/forum/160500/how-can-you-share-data-between-servlets).\
- Things communicate with messages.\
- Things fail.\

\

Erlang can handle hundreds of thousands, even millions, of processes in parallel with a small memory footprint. We can do same in C or Java, but we would struggle when scaling the system to hundreds of thousands of concurrent events. So Erlang is one of the most powerful concurrency [models available](https://www.mindstick.com/forum/158605/discuss-the-different-hosting-models-available-in-asp-dot-net-core) today.

In order to control a set of parallel [activities](https://www.mindstick.com/interview/2242/differentiate-activities-from-services) Erlang has primitives for [multiprocessing](https://www.mindstick.com/forum/157590/explain-multiprogramming-vs-multiprocessing-vs-multitasking-in-os): spawn BIF is used to start a parallel process and send a message to a process through send and receives messages through the receive. spawn will start the execution of a parallel process and returns an identifier that can be used to send messages and [receive messages](https://answers.mindstick.com/qa/95298/why-do-i-still-receive-messages-from-someone-i-blocked-on-telegram-and-how-do-i-stop-this) from the process.

##### Syntax of spawn method:

spawn(Module, Function, Arguments)

After executing this it will creates new process and returns process identifier, and

we refer to as a Pid. [Parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) of spawn/3 BIF:

Module: For that spawn BIF will create new process.

Function: It is the exported function of the module.

Arguments: It is a list of arguments.

Example: spawn(module, function, [arguments]).

##### Syntax for send a message:

> ##### Pid ! Message.
>
> ##### Pid is a process identity and Message is the message which is to be sent to Pid.
>
> ##### Example: Pid ! {v1, 32}.
>
> ##### That means message {v1, 32} is sending to the process Pid. Before sending the
>
> ##### message all the arguments will be evaluate.
>
> ##### \
> Syntax for receive messages:

> ##### receive Message.

If the message {v1, 32} is sent from the sender side then it is received in Message, and after [pattern matching](https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching) will be done.

For complete [understanding](https://yourviews.mindstick.com/view/88474/why-understanding-yourself-is-so-difficult) we will create a simple echo process that will returns a message sent to it:

> ```
> -module(foo).-export([start/0, myfun/0]).start() -> spawn(foo, loop, []).myfun() ->  receive{From, Message} ->
>  From ! Message, myfun()
>   end.
> ```
>
> In the above example spawn(foo, myfun, []) causes the function represented by foo:myfun() to be evaluated in parallel with the calling function. So the [evaluation](https://yourviews.mindstick.com/view/81125/time-for-evaluation-of-traditions) will be like this:

```
Pid1 = foo:start()Pid1 ! {self(), hello}
```

and the whole process doing is a parallel process. Process will start and message {self(), hello} will be sent to the process Pid, and self() BIF will returns the Pid (process identifier) of the current process.

---

Original Source: https://www.mindstick.com/articles/12001/concurrent-programming-in-erlang

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
