Threads | The use of threads and four implementation methods

1. Thread implementation

1. User-level threads

The overhead is small, and user space can create multiple ones. The disadvantage is that the kernel cannot sense the existence of multiple user-level threads and treats it as only one thread, so it will only provide one processor.

2. Kernel-level threads

Slightly more expensive than user-level and can take advantage of multiple processors.

3. Combination-level threads

Implementation of threads in Linux on different platforms:

The mechanism by which Linux implements threads is very unique. From the kernel's perspective, it does not have the concept of threads. Linux implements all threads as processes. The kernel does not prepare special scheduling algorithms or define special data structures to represent threads. Instead, a thread is viewed simply as a process that shares certain resources with other processes. Each thread has its own unique task_struct, so in the kernel, it looks like an ordinary process (it is just that the thread shares certain resources, such as address space, with some other processes).

2. Use of threads

2.1 Thread library interface—pthread function

1)pthread_create

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                        void *(*start_routine) (void *), void *arg);

pthread_create() creates a new thread, similar to the fork function that creates a new process


Parameter 1: pthread_t *thread: A pointer to a pthread_t type data. When a thread is created, an identifier will be written to the variable pointed to by this pointer. We use this identifier to refer to the new thread; thread: Receive the created new thread
. The ID of the thread, according to different ID names, manages the corresponding thread.
Parameter two: const pthread_attr_t *attr
attr: Specifies the attributes of the thread. Generally, no special attributes are set. Set this parameter to NULL;
Parameter three: void *( *start_routine) (void *): The parameter is a void type pointer, and the return value is also the function address of the pointer to void. Tells the new thread to start execution from the location pointed to by this function pointer.
start_routine: The specified thread function that the thread will start
Parameter four: void *arg: Parameter pointer passed to the thread function
arg: Parameters passed to the thread function

//Return 0 on success, error code on failure

2)pthread_join

int pthread_join(pthread_t thread, void **retval);

pthread_join() waits for the thread specified by thread to exit. If the thread does not exit, this method blocks. Equivalent to the wait function used in the process to collect exit information of child processes.

Parameter one: pthread_t thread: specifies the thread to be waited for (pthread_t variable name is used here),
parameter two: void **retval: is a pointer, which points to another pointer, which points to the exit information of the thread.

//Return 0 on success, error code on failure

3)pthread_exit

int pthread_exit(void *retval);

pthread_exit() A thread terminates execution by calling this function. Equivalent to calling the exit function when the process ends.

Parameters: void *retval: A pointer to the thread exit information or a pointer to an object returned when the thread exits. The object cannot be a local object, because after this function ends, the local variable does not exist, which will cause the program to leakage.

Guess you like

Origin blog.csdn.net/weixin_53472334/article/details/132327736