In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
How to analyze Linux multithreaded reentrant function, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Reentrant and Thread-safe
In a single-threaded program, the whole program is executed sequentially, and a function can only be called by one function at a time, but in multithreading, because of concurrency, a function may be called by multiple functions at the same time. At this time, this function becomes a critical resource, which can easily cause the interaction between the processing results of calling functions. If the result of each call of a function in a multithreaded concurrent environment is uncertain, we say that the function is "non-reentrant" / "thread-unsafe". In order to solve this problem, the POSIX multithread library proposes a mechanism to solve the problem of privatization of thread data in a multithreaded environment. The main idea of this mechanism is to use synchronization and mutual exclusion to maintain a table with different values of the same name, which maintains each thread's own resource address, ostensibly the same variable, but in fact, the address of this variable is different in different threads. This ensures that each thread is actually using its own resources, implementing "thread-safe".
In fact, with the gradual popularity of multithreaded programs, in addition to this method of using system mechanisms to protect thread private data, some people have rewritten some multithread library functions. The main feature of these functions is to achieve the separation of algorithm and data. The internal function is only responsible for implementing the algorithm, and the required data is passed in by the thread, thus ensuring the multi-thread safety of the function.
Eg
Char * asctime (const struct tm * tm); char * asctime_r (const struct tm * tm, char * buf); / / this is the thread-safe version of asctime with the suffix _ r, but due to different interfaces, it will take some time to popularize the completely rewritten function.
At present, it is more likely to use _ REENTRANT to modify the original function. If this macro is defined at compile time, the related library functions will be compiled into the "thread-safe" version.
Model
If you want to view the man manual for these functions, you can install the relevant man manual
Pthread_key_t key / / create a keypthread_once_t once_key to protect the private resources of a thread / / create an once_key to initialize key, which requires PTHREAD_INIT_ONCE to be assigned Otherwise, the result is uncertain pthread_key_create () / / create keypthread_once () / / initialize keypthread_getspedifc () / / get the address of thread private resource from key table pthread_setspedifc () / / put the address of thread private resource into key. Examples
On the surface, every function that calls reverse () will get the address of rev. In fact, this rev address is different in different threads. Once a thread calls the reverse () function, the function will first go to the table identified by key to search whether the function has been called before. If so, the rev address belonging to this thread in the table will be returned. If not, the rev will be assigned. And register the thread and its exclusive rev address in the table, making reverse () a reentrant function.
# include # include pthread_key_t key; pthread_once_t once_key=PTHREAD_ONCE_INIT; # ifdef _ REENTRANT void myDestructor (void*p) {free (p);} void myCreateKey (void) {/ / create key pthread_key_create (& key,myDestructor);} # endif char* reverse (char* buf,int len) {# ifdef _ REENTRANT / / initialize key pthread_once (& once_key,myCreateKey) / / get a thread-specific data from key char* rev= (char*) pthread_getspecific (key); if (NULL==rev) {rev= (char*) malloc (len+1); / / put thread-specific data into key pthread_setspecific (key,rev);} # else static char rev [100]; # endif bzero (rev,sizeof (rev)) / flip buf while (len--) rev [len] = * buf++; return rev;} void* fcn1 (void* p) {while (1) {char buf [100] = "123456789"; printf ("[% lu]:% s\ n", pthread_self (), buf); char* rev=reverse (buf,strlen (buf)); sleep (1) Printf ("[% lu]:% s\ n", pthread_self (), rev);}} void* fcn2 (void* p) {while (1) {char buf [100] = "abcdef"; printf ("[% lu]:% s\ n", pthread_self (), buf); char* rev=reverse (buf,strlen (buf)); sleep (2) Printf ("[% lu]:% s\ n", pthread_self (), rev);}} int main (int argc, const char * argv []) {pthread_t tid [4]; pthread_create (& tid [0], NULL,fcn1,NULL); pthread_create (& tid [1], NULL,fcn2,NULL); pause (); return 0 } after reading the above, have you mastered how to analyze Linux multithreaded reentrant functions? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.