Home > OS >  Understanding of the term "thread"
Understanding of the term "thread"

Time:06-09

I have a trouble understanding of the term "thread".

Consider C11, 5.1.2.4 Multi-threaded executions and data races, 1:

Under a hosted implementation, a program can have more than one thread of execution (or thread) running concurrently.

Question: does "thread of execution" (or "thread") implies only a thread created using thrd_create (<threads.h>) or main (hosted environment) or <name of the function called at program startup> (freestanding environment)?

Reason of the question: the following questions:

  • Is thread created via call of interrupt handler a thread in a C sense?
  • Is thread created using your own mechanism (e.g A Minimal User-Level Thread Package) a thread in a C sense?
  • Is thread created using pthread_create (i.e. POSIX thread) a thread in a C sense?
  • Is thread created using CreateThread/_beginthread a thread in a C sense?

CodePudding user response:

Understanding of the term "thread"

"Thread" is a word with many definitions. What it means depends on whose definition you're using, and sometimes one person's "thread" looks nothing like another person's "thread" (e.g. for GPUs a "thread" is often not a "thread of execution" at all and is more like a CPUs "SIMD lane").

If you try to find the common features of different definitions of "thread" used for computers (and ignore outliers, and ignore its use for textiles - "thread of cotton"); you might end up with something like "a thread is a set of instructions (with control flow) that appears to (but might not literally) execute concurrently with other threads".

Is thread created via call of interrupt handler a thread in a C sense?

Interrupts are normally considered "(involuntary) control flow change" and not a thread (e.g. like a thread calling a function, just not as a normal/voluntary part of the program).

Is thread created using your own mechanism (e.g A Minimal User-Level Thread Package) a thread in a C sense?

In the C sense, yes. From the perspective of code using the user-level thread package it's "threads" (and from the perspective of the OS/kernel it might be considered a single thread).

Is thread created using pthread_create (i.e. POSIX thread) a thread in a C sense?

Yes, pthread_create() is one way to create a thread in the C sense.

Is thread created using CreateThread/_beginthread a thread in a C sense?

Yes, these are just more ways to create a thread in the C sense (it's just that they're not as portable). In some cases pthread_create() could be considered a "more portable" wrapper around whatever the OS provides (e.g. CreateThread() on Windows, clone() on Linux, etc).

Ironically, thrd_create() that was introduced in C11 can be considered an "even less portable wrapper", because most people using C are using older versions of C (e.g. C90). This is partly because some platforms (Windows) don't officially support any later version of C (and partly because people that want more modern languages switched to more modern languages).

  • Related