In the process of using the Linux operating system, we often need to manage and configure storage devices. Among them, the resolution and partitioning of storage devices is one of the common management tasks. Understanding how to perform storage parsing and partitioning can help us better utilize storage resources and improve system performance. This article will introduce the relevant knowledge of storage device parsing and partitioning in Linux systems.
When using C/C for multi-threaded programming in Linux systems, the most common problem we encounter is multi-threaded reading and writing of the same variable. In most cases, such problems are handled through the lock mechanism, but This has a great impact on the performance of the program. Of course, for those data types that the system natively supports atomic operations, we can use atomic operations to process them, which will improve the performance of the program to a certain extent. So for those custom data types whose systems do not support atomic operations, how can we achieve thread safety without using locks? This article will briefly explain how to deal with this type of thread safety issue from the aspect of thread local storage.
1. Data type
In C/C programs, there are often global variables, static variables defined within functions, and local variables. For local variables, there is no thread safety issue, so they are not within the scope of this article. Global variables and static variables defined within functions are shared variables that can be accessed by all threads in the same process, so they have multi-threaded reading and writing problems. When the contents of a variable are modified in one thread, other threads can perceive and read the changed contents. This is very fast for data exchange. However, due to the existence of multi-threads, there may be different contents for the same variable. Two or more threads modify the memory content of the variable at the same time, and there are multiple threads reading the memory value when the variable is modified. If the corresponding synchronization mechanism is not used to protect the memory, then all The data read will be unpredictable and may even cause the program to crash.
If you need a variable that can be accessed by each function call within a thread but cannot be accessed by other threads, a new mechanism is needed to implement it. We call it Static memory local to a thread (thread local static variable), and also It can be called thread-specific data (TSD: Thread-Specific Data) or thread-local storage (TLS: Thread-Local Storage). For this type of data, each thread in the program will maintain a copy of the variable, and it will exist in that thread for a long time. Operations on such variables will not affect other threads. As shown below:
2. One-time initialization
Before explaining thread-specific data, let us first understand one-time initialization. Multi-threaded programs sometimes have such a requirement: no matter how many threads are created, the initialization of some data can only happen once. For example: in a C program, a certain class can only have one instance object throughout the life cycle of the process. In the case of multi-threading, in order to allow the object to be initialized safely, the one-time initialization mechanism is particularly important. . ——In design patterns, this implementation is often called singleton pattern (Singleton). Linux provides the following functions to achieve one-time initialization:
#include // Returns 0 on success, or a positive error number on error int pthread_once (pthread_once_t *once_control, void (*init) (void)); 利用参数once_control的状态,函数pthread_once()可以确保无论有多少个线程调用多少次该函数,也只会执行一次由init所指向的由调用者定义的函数。init所指向的函数没有任何参数,形式如下: void init (void) { // some variables initializtion in here }
In addition, the parameter once_control must be a pointer to a pthread_once_t type variable, pointing to a static variable initialized to PTHRAD_ONCE_INIT. A function std::call_once () with similar functions is provided after C 0x, and its usage is similar to this function.
3. Thread local data API
The following functions are provided in Linux to operate thread local data
#include // Returns 0 on success, or a positive error number on error int pthread_key_create (pthread_key_t *key, void (*destructor)(void *)); // Returns 0 on success, or a positive error number on error int pthread_key_delete (pthread_key_t key); // Returns 0 on success, or a positive error number on error int pthread_setspecific (pthread_key_t key, const void *value); // Returns pointer, or NULL if no thread-specific data is associated with key void *pthread_getspecific (pthread_key_t key);
The function pthread_key_create() creates a new key for thread-local data and points to the newly created key buffer through key. Because all threads can use the returned new key, the parameter key can be a global variable (global variables are generally not used in C multi-thread programming, but a separate class is used to encapsulate thread-local data, and each variable uses a independent pthread_key_t). The destructor points to a custom function with the following format:
void Dest (void *value) { // Release storage pointed to by 'value' }
只要线程终止时与key关联的值不为NULL,则destructor所指的函数将会自动被调用。如果一个线程中有多个线程局部存储变量,那么对各个变量所对应的destructor函数的调用顺序是不确定的,因此,每个变量的destructor函数的设计应该相互独立。
函数pthread_key_delete()并不检查当前是否有线程正在使用该线程局部数据变量,也不会调用清理函数destructor,而只是将其释放以供下一次调用pthread_key_create()使用。在Linux线程中,它还会将与之相关的线程数据项设置为NULL。
由于系统对每个进程中pthread_key_t类型的个数是有限制的,所以进程中并不能创建无限个的pthread_key_t变量。Linux中可以通过PTHREAD_KEY_MAX(定义于limits.h文件中)或者系统调用sysconf(_SC_THREAD_KEYS_MAX)来确定当前系统最多支持多少个键。Linux中默认是1024个键,这对于大多数程序来说已经足够了。如果一个线程中有多个线程局部存储变量,通常可以将这些变量封装到一个数据结构中,然后使封装后的数据结构与一个线程局部变量相关联,这样就能减少对键值的使用。
函数pthread_setspecific()用于将value的副本存储于一数据结构中,并将其与调用线程以及key相关联。参数value通常指向由调用者分配的一块内存,当线程终止时,会将该指针作为参数传递给与key相关联的destructor函数。当线程被创建时,会将所有的线程局部存储变量初始化为NULL,因此第一次使用此类变量前必须先调用pthread_getspecific()函数来确认是否已经于对应的key相关联,如果没有,那么pthread_getspecific()会分配一块内存并通过pthread_setspecific()函数保存指向该内存块的指针。
参数value的值也可以不是一个指向调用者分配的内存区域,而是任何可以强制转换为void的变量值,在这种情况下,先前的pthread_key_create()函数应将参数
destructor设置为NULL
函数pthread_getspecific()正好与pthread_setspecific()相反,其是将pthread_setspecific()设置的value取出。在使用取出的值前最好是将void转换成原始数据类型的指针。
四、深入理解线程局部存储机制
\1. 深入理解线程局部存储的实现有助于对其API的使用。在典型的实现中包含以下数组:
pthread_key_create()返回的pthread_key_t类型值只是对全局数组的索引,该全局数组标记为pthread_keys,其格式大概如下:
数组的每个元素都是一个包含两个字段的结构,第一个字段标记该数组元素是否在用,第二个字段用于存放针对此键、线程局部存储变的解构函数的一个副本,即destructor函数。
\2. 在常见的存储pthread_setspecific()函数参数value的实现中,大多数都类似于下图的实现。图中假设pthread_keys[1]分配给func1()函数,pthread API为每个函数维护指向线程局部存储数据块的一个指针数组,其中每个数组元素都与图线程局部数据键的实现(上图)中的全局pthread_keys中元素一一对应。
五、总结
使用全局变量或者静态变量是导致多线程编程中非线程安全的常见原因。在多线程程序中,保障非线程安全的常用手段之一是使用互斥锁来做保护,这种方法带来了并发性能下降,同时也只能有一个线程对数据进行读写。如果程序中能避免使用全局变量或静态变量,那么这些程序就是线程安全的,性能也可以得到很大的提升。如果有些数据只能有一个线程可以访问,那么这一类数据就可以使用线程局部存储机制来处理,虽然使用这种机制会给程序执行效率上带来一定的影响,但对于使用锁机制来说,这些性能影响将可以忽略。更高性能的线程局部存储机制就是使用__thread,这个以后再讨论。
需要C/C++ Linux服务器开发学习资料私信“资料”(资料包括C/C++,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享
本文介绍了Linux系统中存储设备解析和分区的相关知识,包括使用fdisk命令、使用parted命令、使用mkfs命令等。了解这些知识,可以帮助我们更好地管理和配置存储设备,优化系统性能。希望读者能够根据实际需求选择适合自己的方法,并加以应用。
The above is the detailed content of Easily master Linux storage parsing and partitioning skills. For more information, please follow other related articles on the PHP Chinese website!