Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Thread Local variable use and Multithreaded Development

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "thread local variable use and multithreaded development example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "thread local variable use and multithreaded development example analysis" this article.

I. Overview

Nowadays, multi-threaded development is becoming more and more important in the multi-core era, and multi-threading has many advantages (and disadvantages, of course) over multi-processes. In the libraries of early C #, many functions were thread-unsafe because static variables were used internally, such as char * strtok (char * s, const char * delim). There is a static pointer inside the function. If multiple threads call this function at the same time, strange results may occur, which is certainly not what we want. Now LINUX has a thread-safe version of the interface for this function: char * strtok_r (char * s, const char * delim, char * * ptrptr), which avoids the conflict problem of multiple threads accessing at the same time. In fact, if you keep the strtok () / 2 interface unchanged while ensuring thread safety, another solution is to use thread local variables.

There are two ways to use thread local variables, one is a little more troublesome, the other is relatively simple, here is an introduction (take LINUX as an example)

Second, the use of thread local variables

The more troublesome methods mainly use three functions: pthread_once (pthread_once_t*, void (* init_routine) (void)), pthread_key_create () / 2, pthread_setspecific () / 2, pthread_getspecific () / 1, in which pthread_once can guarantee that the init_routine function is called only once in the whole process space (it solves the problem that mutexes and initialization codes are initialized only once in a multithreaded environment) One of the parameters of pthread_key_create refers to a destructor pointer that is called when a thread terminates and can only be called once for a given key in a process; pthread_sespecific and pthread_getspecific are used to store and get the value associated with a key. Examples are as follows:

Pthread_key_t key; pthread_once_t once = PTHREAD_ONCE_INIT; static void destructor (void * ptr) {free (ptr);} void init_once (void) {pthread_key_create (& key, destructor);} static void * get_buf (void) {pthread_once (& once, init_once); if ((ptr = pthread_getspecific (key)) = NULL) {ptr = malloc (1024); pthread_setspecific (key, ptr) } return (ptr);} static void * thread_fn (void * arg) {char* ptr = (char*) get_buf (); sprintf (ptr, "hello world"); printf ("> >% s\ n", ptr); return (NULL);} void test (void) {int I, n = 10; pthread_t tids [10]; for (I = 0; I

< n; i++) { pthread_create(&tids[i], NULL, thread_fn, NULL); } for (i = 0; i < n; i++) { pthread_join(&tids[i], NULL); } } 另外,还有一个更加简单使用线程局部变量的方法:__thread 修饰符, (在WIN32平台下需要用: __declspec(thread) 修饰符,WIN32的东东总得要多写几笔,呵呵),于是上述代码可以修改如下: static void *get_buf(void) { static __thread void *ptr = malloc(1024); return (ptr); } static void *thread_fn(void *arg) { char *ptr = (char*) get_buf(); sprintf(ptr, "hello world"); printf(">

>% s\ n ", ptr); return (NULL);} void test (void) {int I, n = 10; pthread_t tids [10]; for (I = 0; I

< n; i++) { pthread_create(&tids[i], NULL, thread_fn, NULL); } for (i = 0; i < n; i++) { pthread_join(&tids[i], NULL); } } 看到没有,这段代码比前面一个简单许多,但却有一个问题,它存在内存泄露问题,因为当线程退出时各个线程分配的动态内存(ptr = malloc(1024)) 并没有被释放。 三、用ACL线程接口操作线程局部变量 为了解决上述问题,ACL库中实现了线程局部变量的简单释放功能:acl_pthread_atexit_add(void *arg, void (*free_callback)(void*)),修改上述代码如下: static void free_fn(void *ptr) { free(ptr); } static void *get_buf(void) { static __thread void *ptr = malloc(1024); acl_pthread_atexit_add(ptr, free_fn); return (ptr); } static void *thread_fn(void *arg) { char *ptr = (char*) get_buf(); sprintf(ptr, "hello world"); printf(">

>% s\ n ", ptr); return (NULL);} void test (void) {int I, n = 10; pthread_t tids [10]; for (I = 0; I < n; iTunes +) {acl_pthread_create (& tids [I], NULL, thread_fn, NULL);} for (I = 0; I < n; iDex +) {acl_pthread_join (& tids [I], NULL);}}

Ok, all problems have been solved. Careful readers will find that pthread_create and pthread_join are prefixed with a prefix: acl_, this is because the ACL library encapsulates the thread library to adapt to the use of thread libraries on different platforms (UNIX, WIN32). This example is cross-platform and is also available under WIN32.

The above is all the contents of the article "sample Analysis of Thread Local variable use and Multithreaded Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report