Trivial concurrency exercises

I have been working on concurrency exercises and I can not get my head around them. This problem is supposed to be for the confused newbie Gopher but it seems very advance. Could someone solve this problem and give me a lift on how to approach algorithms in Golang with concurrency? Thanks

###Exercise 1: The Daily Walk
Every morning, Alice and Bob go for a walk, and being creatures of habit, they follow the same routine every day.
First, they both prepare, grabbing sunglasses, perhaps a belt, closing open windows, turning off ceiling fans, and pocketing their phones and keys.
Once they’re both ready, which typically takes each of them between 60 and 90 seconds, they arm the alarm, which has a 60 second delay.
While the alarm is counting down, they both put on their shoes, a process which tends to take each of them between 35 and 45 seconds.
Then they leave the house together and lock the door, before the alarm has finished its countdown.
Write a program to simulate Alice and Bob’s morning routine.
####sample output
Let’s go for a walk!
Bob started getting ready
Alice started getting ready
Alice spent 72 seconds getting ready
Bob spent 76 seconds getting ready
Arming alarm.
Bob started putting on shoes
Alarm is counting down.
Alice started putting on shoes
Alice spent 37 seconds putting on shoes
Bob spent 39 seconds putting on shoes
Exiting and locking the door.
Alarm is armed.

This is just a quick and rough solution, but it creates the same output and should get the idea across assuming you can find your way through my messy code^^

IMO the most important idea (coming from “simpler” languages like JS, PHP etc, I don’t know anything about “traditional” multithreading) is when you block and when you don’t.

  • When you call a function (“traditionally”), you block, ie you wait for it to finish and then move along.
  • When you call go aFunction() you don’t wait for it to finish, basically saying “you go ahead and do your thing”, meanwhile breezing along.

In your example you then need to “sync” them, ie wait till everyone is finished. My solution is probably not the most elegant one, but the idea is that when you receive from a channel, you block until you get an answer. (I just wait for either one of them to respond and then wait for the other one)

All the above is assuming you know a little about go, functions, channels etc; if not, please ask and specify :smile:
There are probably loads of good writings on this, I can recommend this one, it’s a very quick read.


Your code will get me off the ground. It is better than mines. The sync part is what I had trouble understanding. Also how to code the timer part. Thanks alot.

Quick one with sync.WaitGroup

This implementation is very close to what I did the first time. Then I decided to change it a bit and came up with this solution

I’m open to suggestions.

Somethings I learned:

  • Time.UnixNano() is great for precision
  • You can re-use the same instance of sync.WaitGroup as long as you keep adding “things to wait” and calling Done() the right amount of times.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.