## Parallel Programming: Practical examples of a thread

Here is a simple problem: A boy and a girl toss a ball back and forth to each other. Assume that the boy is one thread (node) and the girl is another thread, and b is data.

Boy = m

Girl = f

Ball = b

• m has b
1. m throws b –> f catches b
• f has b
1. f throws b –> m catches b

Assuming we could drop the ball, and holding everything else constant.

• m has b
1. m throws b –> f catches b
2. m throws b –> f drops b
1. f picks up the dropped b
• f has b
1. f throws b –> m catches b
2. f throws b –> m drops b
1. m picks up the dropped b

Suppose you add a third player.

Boy = m

Girl = f

Ball = b

3rd player = x

• m has b
1. m throws b –> f catches b
2. m throws b –> x catches b
• f has b
1. f throws b –> m catches b
2. f throws b –> x catches b
• x has b
1. x throws b –> m catches b
2. x throws b –> f catches b

Assuming we could drop the ball, and holding everything else constant.

• m has b
1. m throws b –> f catches b
2. m throws b –> f drops b
1. f picks up the dropped b
3. m throws b –> x catches b
4. m throws b –> x drops b
1. x picks up the drooped b
• f has b
1. f throws b –> m catches b
2. f throws b –> m drops b
1. m picks up the dropped b
3. f throws b –> x catches b
4. f throws b –> x drops b
1. x picks up the dropped b
• x has b
1. x throws b –> m catches b
2. x throws b –> m drops b
1. m picks up the dropped b
3. x throws b –> f catches b
4. x throws b –> f drops b
1. f picks up the dropped b

Will that change the thread models? What if the throwing pattern is not static; that is, the boy can throw to the girl or to the third player, and so forth?

In this example: Yes, there is an additional thread that gets added, because each player is a tread that can catch or drop a ball.  Each player is a thread on its own, transferring data ‘b’ amongst them and throwing the ‘b’ is locking the data before transferring and catching ‘b’ is unlocking the data.  After the ball is dropped (maybe calculated randomly), the player with the ball now has to pick it up, which can be equivalent to analyze the data based on a certain condition that is met like account balance is < 500 or else.  The model changes with the additional player because each person has a choice to make now on which person should receive the ball next, which is not present in the first model when there were two threads.  If there exists a static toss like

• f –> m –> x –> f

Then the model doesn’t change, because there is no choice now.