In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to analyze specific.c based on Linux thread 2.0.1 thread source code, the content of the article is of high quality, so the editor will share it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
This file is the implementation of thread private data. There is an array in the thread tcb that holds a series of key pair values. Thus, the private data storage of the thread is realized. When a thread wants to have its own data, it first gets a key and then saves a key pair value in tcb.
/
/ * Thread-specific data * /
# include
# include
# include "pthread.h"
# include "internals.h"
Typedef void (* destr_function) (void *)
/ * Table of keys. , /
Struct pthread_key_struct {
Int in_use; / * already allocated? * /
Destr_function destr; / * destruction routine * /
}
Static struct pthread_key_struct pthread_ Keys [PTHREAD _ KEYS_MAX] =
{{0, NULL}}
/ * Mutex to protect access to pthread_keys * /
Static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER
/ * Create a new key * /
/ / create a key
Int _ _ pthread_key_create (pthread_key_t * key, destr_function destr)
{
Int i
/ / add lock
Pthread_mutex_lock & pthread_keys_mutex)
/ / find a free item from the list
For (I = 0; I)
< PTHREAD_KEYS_MAX; i++) { if (! pthread_keys[i].in_use) { pthread_keys[i].in_use = 1; pthread_keys[i].destr = destr; // 找到则解锁并返回键值 pthread_mutex_unlock(&pthread_keys_mutex); *key = i; return 0; } } // 找不到则解锁 pthread_mutex_unlock(&pthread_keys_mutex); return EAGAIN; } weak_alias (__pthread_key_create, pthread_key_create) /* Delete a key */ // 删除一个键对应的项 int pthread_key_delete(pthread_key_t key) { pthread_mutex_lock(&pthread_keys_mutex); if (key >= PTHREAD_KEYS_MAX | |! pthread_ Keys [key] .in _ use) {
Pthread_mutex_unlock & pthread_keys_mutex)
Return EINVAL
}
Pthread_ Keys [key] .in _ use = 0
Pthread_ Keys [key] .destr = NULL
Pthread_mutex_unlock & pthread_keys_mutex)
Return 0
}
/ * Set the value of a key * /
/ / the value corresponding to the associated key
Int _ _ pthread_setspecific (pthread_key_t key, const void * pointer)
{
Pthread_t self = thread_self ()
If (key > = PTHREAD_KEYS_MAX) return EINVAL
Self- > p _ specific [key] = (void *) pointer
Return 0
}
Weak_alias (_ _ pthread_setspecific, pthread_setspecific)
/ * Get the value of a key * /
Void * _ pthread_getspecific (pthread_key_t key)
{
Pthread_t self = thread_self ()
If (key > = PTHREAD_KEYS_MAX)
Return NULL
Else
Return self- > p _ specific [key]
}
Weak_alias (_ _ pthread_getspecific, pthread_getspecific)
/ * Call the destruction routines on all keys * /
/ / call the destr function in the pthread_keys array one by one, and take the value associated with the thread as the parameter
Void _ pthread_destroy_specifics ()
{
Int i
Pthread_t self = thread_self ()
Destr_function destr
Void * data
For (I = 0; I)
< PTHREAD_KEYS_MAX; i++) { // 销毁时执行的函数 destr = pthread_keys[i].destr; // 获取键对应的值 data = self->P_ specific[i]
/ / execution
If (destr! = NULL & & data! = NULL) destr (data)
}
}
On how to analyze specific.c based on Linux thread 2.0.1 thread source code analysis here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.