The Event Loop simplified

It's the white time of the year, the horrible Christmas is again upon us. All sit around the table and tell smiling fake stories of their wonderful lives. But before that can happen, dinner and house are to be prepared for the festivity.

Meet the family: MamaBear, Brother1 and Brother2. A simple normal family. And a lot of things to prepare, the tree, the food, the presents. Like in all classic families, organization is key.

First scenario. MamaBear is living in Communist Romania and off she goes to buy the tasteless cakes, the Christmas tree from the...from whatever bastard has cut down trees this year, and last but not least, all the meager things one has learned to do without from the general store - a store only in name not in products. All in all, three places to go to. Three lines to stand in freezing temperatures and no way of knowing which line is faster or a dead end. A kind of a lottery. She will probably stand first in line closer to home.

After listening to uncle Ilie cursing the regime for two hours straight, MamaBear picks up the colorless cakes and moves on, snow above the ankles, to the next target: the tree. Damn surprise. This year, due to the hard economic times - Brother1 and Brother2 say there were always hard times, but whoever listens to them, anyway - there are plenty of wood choppers but barely a soul willing to carry a dead tree inside their home. After fifteen minutes, a little small talk, a price negotiation which leads to the wood costing more than the man asked for it in the first place, MamaBear is heading to the general store, where the same cake scenario takes place only here aunt Ana is priding herself with her smartest nephews God has given eyes to on this planet. Stories nobody believes but good entertainment, nonetheless.

I've spent close to five hours listening to these fools' ramblings and walking between shops, but I haven't actually spent more than two hours in any single place, she comes to realize. What if I Groundhog this Day1 and start this business all over again.

So MamaBear is now a Bear with a Plan. These useless kids might actually contribute something in the house! Brother1 will buy the cakes, Brother2 will go to the general store, and I, as the legitimate empress of the house and one knowledgeable in negotiation skills will take care of the tree. This turns out to work quite well. Same amount of time spent waiting in lines overall, but now split between three souls. No economy achieved overall, but a better deal for MamaBear, though at night the Brothers are grumbling of hunger, raising complaints about how the other kids are playing all day, while we work the mines and disturb her sleep. Good plan, but it can be improved.

Day3. MamaBear goes by herself again. On the first line uncle Ilie again is desecrating the holy sanctity of the regime, but this time she agrees with him, Yes, yes, Ilie, yes, a bunch of psychopaths!... and thieves, yes... and they stole your pig last summer... what?! Well, anyway, that convinces Ilie to keep the line for her while she quickly moves, as quickly as her galoshes allow, to the general store. Sure Ana, sure...your nephews are the smartest....and the most beautiful...and no, of course they don't swear... nor spit, God no!. Ana keeps the line for her here, too. Quick, to the tree! Some better negotiation tricks and half an hour later MamaBear picks the tree at its original asking price. With approximately one and a half hours remaining for each of the other lines, she quickly galoshes between uncle Ilie and aunt Ana. She wants to be there at the exact moment her turn will come, but not knowing which will come first. Smart thinkin', ma! Three hours later, a lot of back and forth between these the lines, with the forest after after her - she says someone will switch the tree with a more plain one if she leave it behind - she's done it! She saved two hours compared to the day before, even though she is now beat.

Day4. MamaBear thinks things better this time. What if, she says to herself, what if I cut these two good-for-nothings out of the equation and talk directly to the store manager. Maybe even phone her directly right now. I've known her since we were dirty kids playing outside with... with nothing, we've had nothing to play with, actually, but we invented games nonetheless. She'll give me a call when stuff is available. That way, I don't have to stay on any line nor sloshs through the snow all day long. Free to stay in the cold coziness of my apartment and employ my swearing skills agains whomever crosses my path.

Oh, look, PapaBear is also in this picture! He lazily smokes cigar after cigar on his armchair. That's why we haven't noticed him behind that thick haze. Nevermind. Nevermind him. He does nothing anyway, only gives orders. Some kind of an idle process.

So what about all this coming and going? On Day1 MamaBear acts a single process, doing tasks in sequence. First come, first serve, regardless of how long a task might take. She is blocked2 by the first line she takes and can't react to developments on those other lines. If the wood choppers throws in the axe and curses their luck while MamaBear is still waiting on line1, then there will be no tree in the house this Christmas - not a bad thing, actually. If line3 goes a lot quicker than line1 in the first part of the day but a lot slower in the second part, then the optimum Bear solution would be to wait on that line first.

On Day2 MamaBear enlists her children. Her time spend freezing is now reduced significantly. On the whole, though, the time spent by the family, minus PapaBear who still does nothing except smoke, is still the same plus the time for the Brothers to dress up and walk the snowy paths. So, actually, a bit longer even. If the wood choppers decide this is really a bad bad Christmas and not come at all, MamaBear would have gone out for nothing. Three people waiting on two lines. This is a classical use of separate threads3 to split a problem in multiple pieces and run them at the same time. If some things are to be had both at the cakes shop and at the general store (like sugar, for example), there would be some kind of communication need between the family members as they don't want to spend the money they don't have already buying the exact same product. Something to think about.

On Day3, when MamaBear goes from line to line she saves some time, but she never actually rests. No time to hear uncle Ilie blabbering about whatever, no time to rest her legs. She's waiting in line, but busily. A classic busy-waiting4 technique when you check if a resource is available time and time again, using CPU time with no purpose.

On Day4 MamaBear enlists the help of outside divinity. Now somebody else will call(back) her when the line has reached her, granted that that the products she actually wants are in store - but there is another divinity for that which we won't cover here. If all the uncles and aunts in the world would just do the same, things would be easier. No endless lines, no time wasted, more family time. But no, uncle Ilie and his kind want to see things with their own eyes, afraid the store manager might drink his drinks and recognize nothing. So he employs the busy-waiting technique or blocking the hole house with his long absence. But MamaBear here uses the event notification or the event demultiplexer technique, the soul of the event loop5.

Wait, PapaBear's hands are moving. He's finally contributing to the spirit of the holidays! He's grabbing something...he's....he's opening a bottle of the good stuff, as he always says. At least someone is having a good time this Christmas.

[1] - Groundhog Day, the 1993 movie where Phil (Bill Murray) is caught in an endless loop, reliving the same day over and over again and gets to do things a little bit differently every time until he can break the bad spell.
[2] - A process that is Busy-waiting is waiting for some event, such as a resource becoming available or the completion of an I/O operation,
[3] - A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler,
[4] - Busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available.
[5] - The event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program.