Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

Date:

Share post:

So, I was messing around with Go the other day, and I had this kinda silly idea: What if I made two Goroutines “fight” each other? Like, not really fight, but, you know, compete in some way. I called it “gorilla fights gorilla” because, well, Goroutines… gorillas… get it? Anyway, here’s how it went down.

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

The Setup

First, I needed a basic Go environment. I already had Go installed, so I just created a new directory and a new Go file called . Nothing fancy.

The “Fight”

I needed something for the Goroutines to compete over. I decided on a simple counter. One Goroutine would try to increment the counter, and the other would try to decrement it. A classic tug-of-war!

Here is the function that I create for running each Goroutine:


func run(name string, increment bool, counter int, done chan bool) {

for i := 0; i < 1000; i++ {

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

if increment {

counter++

*(name, " Increment, now counter is",counter)

} else {

counter--

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

*(name, " Decrement, now counter is",counter)

done <- true

Then, I start to write main function:


func main() {

counter := 0

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

done := make(chan bool)

go run("Gorilla 1", true, &counter, done)

go run("Gorilla 2", false, &counter, done)

<-done

<-done

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

*("Final counter:", counter)

I spun up two Goroutines. Gorilla 1 was the incrementer, and Gorilla 2 was the decrementer. They both had access to the shared counter variable. I need to make sure only one Goroutine messes with the counter at a time, so each Goroutine add and minus by one.

After launching the Goroutines, I used the done channel to wait for both of them to finish. This is important, without this, the program would just exit before the Goroutines even got a chance to run!

The Results

I ran the code, and … it worked, and it would print who is doing what now! Each time I run it, I get a different result for who “wins”. Because, this is decided by the scheduler. Sometimes Gorilla 1 gets ahead, sometimes Gorilla 2 does. It’s all down to the whims of the Go scheduler.

Wrapping Up

So, that was my silly little “gorilla fights gorilla” experiment. It wasn’t groundbreaking, but it was a fun way to play around with Goroutines and see how they interact with shared resources. And it reinforced the importance of always remembering that concurrent programming can be unpredictable!

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related articles

Best Super Bowl Halftime Show Predictions See Potential Lineup Here

Alright so first I grabbed my coffee and opened Twitter, you know how rumors start popping up months...

Whats fun at Longview Beach Missouri? Discover popular activities nearby

Man let me tell you about my Sunday out at Longview Beach. Woke up early, grabbed my bag...

Table segunda division latest standings see who leads the race now

So last night I got curious about Spain’s second division standings after watching highlights. Grabbed my laptop thinking...

Where to buy two stroke enduro bikes? Trusted dealers with best off-road machines!

Alright, so today I figured I’d finally pull the trigger on getting a proper two-stroke enduro bike. Been...