Threads Lecture Overview


Threads Lecture Overview

Maria
So today we're talking about threads, right? I remember last week we covered processes, and now we're diving deeper into multitasking within a single process.

Michael
Exactly. Threads let a single process do multiple things at once, like a game running a thread for keyboard input and another for updating the screen. That way, you don't have to switch back and forth sequentially, which slows things down.

Maria
So it's about parallelism? But last week we talked about concurrency too. What's the difference?

Michael
Great question. Concurrency is when multiple tasks make progress over time, but they might not run simultaneously — like in a time-sharing system. Parallelism is when tasks actually run at the same time on different cores. Threads enable both, but for true parallelism you need multicore hardware.

Maria
And why use threads instead of multiple processes? I remember processes have a lot of overhead.

Michael
Exactly. Threads are lighter because they share the same code, data, and files of their parent process. Creating a new process means duplicating all that, but a thread just needs a new stack and registers. That makes thread creation much cheaper in terms of CPU time and memory.

Maria
So that's why word processors can do spell checking as you type without lagging. It's a background thread sharing the document data with the main editor thread.

Michael
Precisely. That's a perfect example of a thread doing a background task. And if you're rendering a video, you can split the image into chunks and send each to a different thread — that's data parallelism. Or you can have task parallelism, where different threads do different things, like one handling input, another rendering, and another saving.

Maria
But is there a limit to how much we can speed up with more threads? I remember Amdahl's Law from somewhere.

Michael
Yes, Amdahl's Law says that speedup is limited by the serial portion of a task. For example, if 75% of a task can be parallelized but 25% must be done sequentially, doubling cores doesn't double performance — you only get about 1.6 times faster. The serial part holds things back.

Maria
So it's not just about throwing more hardware at a problem. The programmer has to design threads carefully. How do they actually create threads? I assume there are system calls involved.

Michael
Right. Operating systems provide thread libraries. For example, POSIX threads (Pthreads) are used in Linux and macOS. Windows has its own API, and Java has built-in thread support. The programmer calls library functions to create threads, and the kernel then maps these user threads to kernel threads, which the OS schedules on cores.

Maria
I've heard about different models for mapping user threads to kernel threads. What are the common ones?

Michael
There are several. The many-to-one model maps many user threads to one kernel thread — but that means no parallelism and a blocked thread blocks all. The one-to-one model maps each user thread to its own kernel thread, giving true parallelism. That's used by Windows and Linux. Then there's many-to-many, where many user threads are multiplexed onto many kernel threads, giving flexibility but added complexity.

Maria
I see. And I think you mentioned something about thread context and the executive thread in Windows. Can you explain that?

Michael
Sure. Each thread has a context — its own set of registers, stack, and a thread ID. The operating system stores this info. In Windows, there's an executive thread structure in kernel space that points to the kernel thread, which then points to the user space thread. This keeps track of scheduling and synchronization. Linux handles threads similarly but calls them tasks and uses a clone system call that lets you share or copy resources.

Maria
That sounds like a lot of detail for programmers. For us in this course, do we need to know all that?

Michael
Not deeply. The key takeaway is that threads allow a process to do multiple things concurrently, they're lighter than processes, and they're supported by the OS through thread libraries. The rest is for implementing them. But understanding these basics helps you appreciate how modern software stays responsive.

Maria
Thanks, that clears things up. So threads are all about efficiency and responsiveness without the overhead of creating new processes.

Michael
Exactly. And as hardware gets more cores, threaded applications can scale better — as long as the serial part doesn't become a bottleneck. That's the balance programmers have to manage.

Ask anything
We've read this whole study set, so questions about it get the best answers.