Home > Software engineering >  When threads are created, do they run in parallel or concurrently?
When threads are created, do they run in parallel or concurrently?

Time:04-23

I’m learning about multi threading and I understand the difference between parallelism and concurrency. My question is, if I create a second thread and detach it from the parent thread, do these 2 run concurrently or in parallel?

CodePudding user response:

Typically (for a modern OS) there's about 100 processes with maybe an average of 2 threads each for a total of 200 threads just for background services, GUI, software updaters, etc. You start your process (1 more thread) and it spawns its second thread, so now there's 202 threads.

Out of those 202 threads almost all of them will be blocked waiting for something to happen most of the time. If 30 threads are not blocked, and you have 8 CPUs, then 30 threads compete for those 8 CPUs.

If 30 threads compete for 8 CPUs, then maybe 4 threads are high priority and get a whole CPU for themselves and 10 threads are low priority and don't get any CPU time because there's more important work for CPUs to do; and maybe 12 threads are medium priority and share 4 CPUs by time multiplexing (frequently switching between threads). How this actually works depends on the OS and its scheduler (its very different for different operating systems).

Of course the number of threads and their priorities changes often (e.g. as threads are created and terminated), and threads block and unblock very often, so how many threads are competing for CPUs (not blocked) is constantly changing. Maybe there's 30 threads competing for 8 CPUs at one point in time, and 2 milliseconds later there's 5 threads and 3 CPUs are idle.

My question is, if I create a second thread and detach it from the parent thread, do these 2 run concurrently or in parallel?

Yes; your 2 threads may either run concurrently (share a CPU with time multiplexing) or run in parallel (on different CPUs); and can do both at different times (concurrently for a while, then parallel for a while, then..).

CodePudding user response:

if I create a second thread and detach it...

Detach is not something your program can do to a thread. It's merely something the program can do to a std::thread object. The object is not the thread. The object is just a handle that your program can use to talk about the thread, and "detach" just grants permission for the program to destroy the handle, while the thread continues to run.

...from the parent thread

Most programming environments do not recognize any parent/child relationship between threads. If thread A creates thread B, that does not give thread A any special privileges or capabilities with respect to thread B that threads C, D, and E don't also have.

That's not to say that you can't recognize the relationship. It might mean something in your program. It just doesn't mean anything to the OS.

...Parallel or concurrently

That's not an either-or choice. Parallel is concurrent.

"Concurrency" does not mean "threads context switching." Rather, it's a statement about the order in which the threads access and update shared variables.

When two threads run concurrently on typical multiprocessing hardware, their actions will be serializable. That is to say, the outcome will be the same as if some single, imaginary thread did all the same things, one-by-one in some particular order.

Two threads are concurrent with each other if the serialization is non-deterministic. That is to say, if the "particular order" in which the things all seem to happen is not entirely determined by the program. They are concurrent if different runs of the program can behave as if the imaginary single thread chose different serializations.

There's also a simpler definition, that usually amounts to the same thing: Two threads are concurrent with each other if both threads are started before either one of them is finished.

Any way you look at it though, if two threads are truly running in parallel with each other, then they are also concurrent with each other.

Do they run in parallel?

@Brendan already answered that one. TLDR: if the computer has more than one CPU, then they potentially can run in parallel. How much of the time they actually spend running in parallel depends on many things though, and many of those things are in the domain of the operating system.

  • Related