Introducing the system calls for thread manipulation in C++
As discussed in Chapter 1, we use threads to execute separate procedures in parallel. They exist only in the scope of a process and their creation overhead is bigger than the thread’s one, so we consider them lightweight, although they have their own stack and task_struct
. They are almost self-sufficient, except they rely on the parent process to exist. That process is also known as the main thread. All others that are created by it need to join it to be initiated. You could create thousands of threads simultaneously on the system, but they will not run in parallel. You can run only n parallel tasks, where n is the number of the system’s concurrent ALUs (occasionally, these are the hardware’s concurrent threads). The others will be scheduled according to the OS’s task-scheduling mechanism. Let’s look at the simplest example of a POSIX thread interface:
pthread_t new_thread; pthread_create...