cd week_five

Journal Entry Week 5

This week we moved deeper into concurrency, and now it’s not just “threads exist,” it’s like “threads can absolutely destroy your program if you’re not careful.” At first, I thought threads were just about doing multiple things at once, but now I’m realizing it’s really about controlling that chaos so everything doesn’t break.

Topics We Covered

  • Threads and concurrency basics
  • Thread creation (pthread API)
  • Race conditions and critical sections
  • Locks (mutexes)
  • Condition variables
  • Lock performance and implementations
  • Concurrent data structures (counters, lists, queues, hash tables)

Explaining the Topics

Threads are basically multiple execution paths inside the same program. Instead of one thing happening at a time, you’ve got multiple things running and sharing memory, which is where things start getting tricky.

The pthread API is how we actually create and manage threads. pthread_create() starts a thread, and pthread_join() waits for it to finish. It felt confusing at first, especially with pointers, but I’m starting to see it as just “launch work --> wait for it to finish”.

Race conditions were probably the biggest concept this week. That’s when multiple threads mess with shared data at the same time and you get random, unpredictable results. Like the counter example—sometimes it works, sometimes it doesn’t.

Locks (mutexes) are basically how we fix that. You “lock” a section of code so only one thread can run it at a time. That creates something called mutual exclusion, which keeps things from breaking .

Condition variables were a bit different—they’re more about threads waiting for something to happen instead of just blocking each other. Like one thread sleeps until another one signals it.

Then we looked at how all of this applies to real data structures. You can just throw one big lock on something or get fancy with multiple locks to improve performance. Turns out more concurrency doesn’t always mean faster.

Least-Understood Topics

The hardest stuff for me was definitely the lower-level lock implementations (like test-and-set, compare-and-swap, etc.). I get the idea that they’re atomic operations, but the actual step-by-step execution and why they work is still kind of fuzzy.

Also, condition variables are still not 100% clear. I understand what they do, but the exact flow of locking, waiting, signaling, and re-checking conditions is confusing.

Nature of My Confusion

What makes sense:

  • Why race conditions happen
  • Why we need locks
  • The general idea of “only one thread at a time”

What doesn’t fully click yet:

  • How atomic instructions actually prevent race conditions at the hardware level
  • Why some locking approaches are inefficient (like spinning vs sleeping)
  • The exact timing of condition variables (like when the lock is released/reacquired)

It feels like I understand the “what” but not always the “why it works under the hood.”

Aha Moments

The biggest “aha” moment was realizing that programs can be indeterminate. That was kind of wild. I’ve always thought programs should give the same result every time, but with threads, that’s just not true. The result literally depends on timing and scheduling .

Also, the idea of a critical section finally clicked. It’s just the part of code that must not be run by multiple threads at the same time. That made everything else (locks, mutexes, etc.) make more sense.

Questions I Still Have

  • How do you decide the “best” locking strategy in real applications?
  • When should you use fine-grained locks vs just one big lock?
  • Are there cases where threads are actually not worth using?
  • How do developers debug race conditions in real life when they’re inconsistent?

I feel like debugging this kind of stuff would be a nightmare.

Connections to Other Classes / Real Life

This connects a lot to programming in general, especially stuff like Java threads or even backend systems. I’ve definitely heard about things like “thread safety” before, but now I actually understand what that means.

It also connects to databases in regard to atomic operations.

Overall, this week made me realize that writing correct concurrent programs is way harder than it looks. It’s not just coding—it’s thinking about timing, scheduling, and all the weird ways things can go wrong.

Comments

Popular posts from this blog

git checkout -b review-markov

Week Força

Week Toosie