Computer Science BS Journal (CST334) : Week 5

   This week in CST334 our lectures covered: Concurrency and Threads, Threads API, Locks, and Lock-based data structure. In this learning journal I'll write and explanation/what I think to be the most important bits of each subject. We also had our midterm this week and I didn't do as good as I wanted to, I definitely need to focus some more on my studies.

Concurrency and Threads

Concurrency is the ability of a system to handle multiple tasks at the same time. Threads are the smallest units of execution within a process. Concurrency allows threads to run independently, improving efficiency, especially on multi-core processors.

Key Takeaways:

  • Concurrency enables better CPU utilization.

  • Threads share the same memory space within a process.

  • Context switching allows threads to appear to run in parallel.

  • Can lead to race conditions if not properly synchronized.


Threads API

Threads APIs provide the functions and mechanisms to create, manage, and synchronize threads. Common APIs include POSIX threads (pthreads) in C, Java Thread class, and threading libraries in other languages.

Key Takeaways:

  • pthread_create, pthread_join (C) or Thread.start(), Thread.join() (Java) are common functions.

  • APIs typically include thread lifecycle management and synchronization primitives (mutexes, condition variables).

  • Thread creation has overhead, so excessive use can degrade performance.


Locks

Locks are synchronization mechanisms that prevent multiple threads from accessing shared resources simultaneously to avoid race conditions.

Key Takeaways:

  • Mutex (Mutual Exclusion) is the most common lock type.

  • Only one thread can hold a lock at a time.

  • Improper use can lead to deadlocks, livelocks, or starvation.

  • Lock granularity matters: fine-grained locks offer better performance but increase complexity.


Lock-Based Data Structures

These are data structures (e.g., queues, stacks) that use locks to ensure thread-safe operations, meaning that multiple threads can safely interact with them without corrupting the data.

Key Takeaways:

  • Use locks internally to synchronize access to data.

  • Performance can degrade with high contention.

  • Simpler to implement than lock-free structures but may block threads.

  • Examples: ConcurrentLinkedQueue, synchronized lists/maps.



Comments

Popular posts from this blog

Computer Science BS Journal: Week 4

Computer Science BS Journal (CST363) : Week 2

Computer Science BS Journal (CST363) : Week 5