怎么基于linuxthreads2.0.1线程源码分析specific.c
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章将为大家详细讲解有关怎么基于linuxthreads2.0.1线程源码分析specific.c,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。该
千家信息网最后更新 2026年11月02日怎么基于linuxthreads2.0.1线程源码分析specific.c
这篇文章将为大家详细讲解有关怎么基于linuxthreads2.0.1线程源码分析specific.c,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
该文件是线程私有数据的实现。在线程tcb里有一个数组,保存了一系列的键对值。从而实现了线程的私有数据存储。线程想拥有自己的数据时,首先获取一个键,然后在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 */
// 创建一个key
int __pthread_key_create(pthread_key_t * key, destr_function destr)
{
int i;
// 加锁
pthread_mutex_lock(&pthread_keys_mutex);
// 从列表中找一项空闲的
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 */
// 关联键对应的值
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 */
// 逐个调用pthread_keys数组中的destr函数,并以线程关联的value为参数
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];
// 执行
if (destr != NULL && data != NULL) destr(data);
}
}
关于怎么基于linuxthreads2.0.1线程源码分析specific.c就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
线程
数据
源码
分析
内容
函数
数组
文章
更多
知识
篇文章
关联
私有
不错
参数
文件
空闲
质量
参考
存储
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网金融科技
互联网佳科技有限公司
金融科技还是互联网
互联网下的科技发展
朗新科技与工业互联网
互联网中国科技
紫鲸互联网科技
互联网科技革命
亿策互联网科技
互联网科技视频素材
互联网科技有哪些公司
岳阳华臻互联网科技有限公司
电子科技大学互联网创业大赛
允成互联网科技有限公司融资
电子商务与互联网科技的关系
黑云互联网科技有限公司
萌小萌互联网科技河南有限公司
同城互联网络科技
互联网科技大会大佬
武汉亿视互联网科技有限公司
咖啡豆
互联网科技保险
奇酷互联网络科技公司
米客互联网科技
互联网科技大会
互联网实体化科技化
电子商务与互联网科技的关系
互联网营销推广黑科技
吉林省码上互联网科技电话
互联网科技企业文化