Shared Mutability: Handle with care!!
Tags: concurrency, database, datastructures, softwaredesign
Published: 2017-03-31 15:28:46

---
title: "Shared Mutability: Handle with care!!"
published: true
description: 
cover_image: https://cdn-images-1.medium.com/max/2000/1*8wMU0ZvkYIqCyw75msQ6Zw.jpeg
tags: Concurrency, Databases, Data Structures, Software Design
---
_This article was first published on Medium. You can take a look at it [here](https://medium.com/techspire/shared-mutability-handle-with-care-351b8fa2f939)_.

Let's bifurcate these 2 words and understand them one by one.
*Shared* and *Mutability*

**Sharing**
Sharing in computer science means sharing of resource like I/O, data etc between multiple processes. In case of data as a resource, sharing may mean permission to read and/or update it by multiple processes.

* _Shared Reading_
Multiple processes are allowed to read a common resource but only one process can modify it at a time.

* _Shared Modification_
Multiple processes can read as well as modify a particular resource at a time.

**Mutability**
Mutability means ability to change. In computer science terms, it may mean any of the following:

* _Changing value of some variable_

```python
a = 5
a = 10        # changing the value of variable
```

* _Modifying a data structure_
In the example below, some nodes of binary tree are removed.
![alt text](https://cdn-images-1.medium.com/max/800/1*vu8VVmvcYJYicfJXxLxerw.png "Logo Title Text 1")

* _Modifying data kept in a data structure_
In the example below, value of fourth node of linked list is changed from 100 to 50.
![alt text](https://cdn-images-1.medium.com/max/800/1*SS7zqOz_i2A2Wp042ikB7g.png)


> _Mutability is fine as long as single process is allowed to modify the data at a time. Sharing is fine as long as multiple processes do shared reading but not shared modification._

Problems come when the above mentioned 2 keywords *Sharing* and *Mutability* come together.
In other words, problems arise when multiple processes are allowed to read as well as modify a common piece of data.

How ????
Let's understand it with the help of an example:
There is a grocery store

```python
groceries = [apple, banana, orange, strawberries, cherries]
```
and as in every grocery store, there is a basket

```python
basket = []
```
and a function to get groceries from grocery store

```python
def get_groceries():
    for item in groceries:
        if item not in basket:
            basket.append(item)
        print basket
```

Looks like a fine piece of code !!
Tom comes | picks up the basket | starts picking groceries from groceries array | puts it in basket.

Life of tom is full of happiness !!

While he was purchasing groceries, Jerry came and met tom. Tom asked Jerry to help him to get groceries. Jerry also started using the `get_groceries` function to put groceries in **Tom's basket**.
![alt text](https://cdn-images-1.medium.com/max/1000/1*FXstOcfLAr2yCDbLRgZDww.png)

By analysing the above example, we can see that since both Tom and Jerry were using the same basket they both saw that banana is not in the basket and hence both of them put banana in it.
Now we have 2 bananas in the basket.

**Basket acts as a shared resource which is being accessed by multiple processes like Tom and Jerry.**

If Jerry would have taken the responsibility of only picking the basket and not putting anything in it then Tom would be only one adding groceries in it.In such a case, above mentioned problem wouldn't have occurred. **[analogous to Shared Reading]**

> _Mutability by single process at a time_


Not only both processes were *sharing* the basket **(common resource)** but also *mutating* it **(adding groceries in the basket)** at the same time. **[analogous to Shared Modification]**

Hence the term **Shared Mutability** !!