Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 24, 2019 12:29 am GMT

Concurrency, parallelism, and the many threads of Santa Claus

Consider the following: Santa brings toys to all the good girls and boys.

There are 7,713,468,100 people in the world in 2019, around 26.3% of which are under 15 years old. This works out to 2,028,642,110 children (persons under 15 years of age) in the world this year.

Santa doesnt seem to visit children of every religion, so well generalize and only include Christians and non-religious folks. Collectively that makes up approximately 44.72% of the population. If we assume that all kids take after their parents, then 907,208,751.6 children would appear to be Santa-eligible.

What percentage of those children are good? Its impossible to know; however, we can work on a few assumptions. One is that Santa Claus functions more on optimism than economics and would likely have prepared for the possibility that every child is a good child in any given year. Thus, he would be prepared to give a toy to every child. Lets assume its been a great year and that all 907,208,751.6 children are getting toys.

Thats a lot of presents, and, as we know, theyre all made by Santas elves at his North China Pole workshop. Given that there are 365 days in a year and one of them is Christmas, lets assume that Santas elves collectively have 364 days to create and gift wrap 907,208,752 (rounded up) presents. That works out to 2,492,331.74 presents per day.

Almost two-and-a-half million presents per day is a heavy workload for any workshop. Lets look at two paradigms that Santa might employ to hit this goal: concurrency, and parallelism.

A sequential process

Suppose that Santas workshop is staffed by exactly one, very hard working, very tired elf. The production of one present involves four steps:

  1. Cutting wood
  2. Assembly and glueing
  3. Painting
  4. Gift-wrapping

With a single elf, only one step for one present can be happening at any instance in time. If the elf were to produce one present at a time from beginning to end, that process would be executed sequentially. Its not the most efficient method for producing two-and-a-half million presents per day; for instance, the elf would have to wait around doing nothing while the glue on the present was drying before moving on to the next step.

Illustration of sequence

Concurrency

In order to be more efficient, the elf works on all presents concurrently.

Instead of completing one present at a time, the elf first cuts all the wood for all the toys, one by one. When everything is cut, the elf assembles and glues the toys together, one after the other. This concurrent processing means that the glue from the first toy has time to dry (without needing more attention from the elf) while the remaining toys are glued together. The same goes for painting, one toy at a time, and finally wrapping.

Illustration of concurrency

Since one elf can only do one task at a time, a single elf is using the day as efficiently as possible by concurrently producing presents.

Parallelism

Hopefully, Santas workshop has more than just one elf. With more elves, more toys can be built simultaneously over the course of a day. This simultaneous work means that the presents are being produced in parallel. Parallel processing carried out by multiple elves means more work happens at the same time.

Illustration of parallel processes

Elves working in parallel can also employ concurrency. One elf can still tackle only one task at a time, so its most efficient to have multiple elves concurrently producing presents.

Of course, if Santas workshop has, say, two-and-a-half million elves, each elf would only need to finish a maximum of one present per day. In this case, working sequentially doesnt detract from the workshops efficiency. There would still be 7,668.26 elves left over to fetch coffee and lunch.

Santa Claus, and threading

After all the elves hard work is done, its up to Santa Claus to deliver the presents -- all 907,208,752 of them.

Santa doesnt need to make a visit to every kid; just to the one household tree. So how many trees does Santa need to visit? Again with broad generalization, well say that the average number of children per household worldwide is 2.45, based on the years predicted fertility rates. That makes 370,289,286.4 houses to visit. Lets round that up to 370,289,287.

How long does Santa have? The lore says one night, which means one earthly rotation, and thus 24 hours. NORAD confirms.

This means Santa must visit 370,289,287 households in 24 hours (86,400 seconds), at a rate of 4,285.75 households per second, nevermind the time it takes to put presents under the tree and grab a cookie.

Clearly, Santa doesnt exist in our dimension. This is especially likely given that despite being chubby and plump, he fits down a chimney (with a lit fire, while remaining unhurt) carrying a sack of toys containing presents for all the households children. We havent even considered the fact that his sleigh carries enough toys for every believing boy and girl around the world, and flies.

Does Santa exist outside our rules of physics? How could one entity manage to travel around the world, delivering packages, in under 24 hours at a rate of 4,285.75 households per second, and still have time for milk and cookies and kissing mommy?

One thing is certain: Santa uses the Internet. No other technology has yet enabled packages to travel quite so far and quite so quickly. Even so, attempting to reach upwards of four thousand households per second is no small task, even with even the best gigabit Internet hookup the North Pole has to offer. How might Santa increase his efficiency?

Theres clearly only one logical conclusion to this mystery: Santa Claus is a multithreaded process.

A single thread

Lets work outward. Think of a thread as one particular task, or the most granular sequence of instructions that Santa might execute. One thread might execute the task, put present under tree. A thread is a component of a process, in this case, Santas process of delivering presents.

If Santa Claus is single-threaded, he, as a process, would only be able to accomplish one task at a time. Since hes old and a bit forgetful, he probably has a set of instructions for delivering presents, as well as a schedule to abide by. These two things guide Santas thread until his process is complete.

A single Santa Claus emoji

Single-threaded Santa Claus might work something like this:

  1. Land sleigh at Timmys house
  2. Get Timmys present from sleigh
  3. Enter house via chimney
  4. Locate Christmas tree
  5. Place Timmys present under Christmas tree
  6. Exit house via chimney
  7. Take off in sleigh

Rinse and repeat another 370,289,286 times.

Multithreading

Multithreaded Santa Claus, by contrast, is the Doctor Manhattan of the North Pole. Theres still only one Santa Claus in the world; however, he has the amazing ability to multiply his consciousness and accomplish multiple instruction sets of tasks simultaneously. These additional task workers, or worker threads, are created and controlled by the main process of Santa delivering presents.

Multiple Santa threads

Each worker thread acts independently to complete its instructions. Since they all belong to Santas consciousness, they share Santas memory and know everything that Santa knows, including what planet theyre running around on, and where to get the presents from.

With this shared knowledge, each thread is able to execute its set of instructions in parallel with the other threads. This multithreaded parallelism makes the one and only Santa Claus as efficient as possible.

If an average present delivery run takes an hour, Santa need only spawn 4,286 worker threads. With each making one delivery trip per hour, Santa will have completed all 370,289,287 trips by the end of the night.

Of course, in theory, Santa could even spawn 370,289,287 worker threads, each flying to one household to deliver presents for all the children in it! That would make Santas process extremely efficient, and also explain how he manages to consume all those milk-dunked cookies without getting full.

An efficient and merry multithreaded Christmas

Thanks to modern computing, we now finally understand how Santa Claus manages the seemingly-impossible task of delivering toys to good girls and boys the world-over. From my family to yours, I hope you have a wonderful Christmas. Dont forget to hang up your stockings on the router shelf.

Of course, none of this explains how reindeer manage to fly.


Original Link: https://dev.to/victoria/concurrency-parallelism-and-the-many-threads-of-santa-claus-27g7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To