DEV Community

Cover image for Shared Mutability: Handle with care!!
saurabh.v
saurabh.v

Posted on

Shared Mutability: Handle with care!!

This article was first published on Medium. You can take a look at it here.

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
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

  • 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

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

groceries = [apple, banana, orange, strawberries, cherries]

and as in every grocery store, there is a basket

basket = []

and a function to get groceries from grocery store

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

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 !!

Top comments (0)