线程本地存储:pthread_key_t
2015-03-25 23:23
316 查看
一、代码
pthread_key_t
[b] pthread_key_create()、pthread_key_delete()[/b]
[b] pthread_setspecific()、pthread_getspecific()
[/b]
pthread_key_t
[b] pthread_key_create()、pthread_key_delete()[/b]
[b] pthread_setspecific()、pthread_getspecific()
[/b]
#include <stdlib.h> #include <stdio.h> #include <pthread.h> //step 1 static pthread_key_t g_key; void* thread_proc(void* arg) { int id = pthread_self(); //step 3 pthread_setspecific(g_key, &id); //step 3 int key = *(int*)pthread_getspecific(g_key); printf("thread 0x%x id is 0x%x\n", pthread_self(), key); return NULL; } int main(int argc, char* argv[]) { pthread_t tid[10]; int i; //step 2 pthread_key_create(&g_key, NULL); for (i=0;i<10;i++) pthread_create(&tid[i], NULL, thread_proc, NULL); for (i=0;i<10;i++) pthread_join(tid[i], NULL); //step 2 pthread_key_delete(g_key); return 0; }二、运行结果
相关文章推荐
- linux中的线程本地存储pthread_key_t和pthread_once
- 线程私有存储空间--pthread_key_t
- 线程存储之pthread_key_create()
- [并发并行]_[任务停止]_[使用Pthread的线程本地存储来停止任务执行]
- pthread_key_t 线程存储相关
- 线程存储pthread_key_create
- [并发并行]_[C/C++]_[使用线程本地存储Thread Local Storage(TLS)-win32线程和pthread线程比较]
- [并发并行]_[C/C++]_[使用线程本地存储Thread Local Storage(TLS)-win32线程和pthread线程比较]
- java之线程本地存储
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
- 线程本地存储 (TLS)
- 几种线程本地存储变量和普通变量的性能比较
- 现代c++之线程本地存储thread_local
- 线程本地存储linux, windows函数对应关系
- 线程本地存储(Windows和Linux) http://blog.csdn.net/wzhwho/article/details/6427596
- DLL, 线程本地存储
- 线程通信原理之一:线程本地存储(TLS)
- C# 线程本地存储 调用上下文 逻辑调用上下文
- boost IO流和本地线程存储
- Java并发编程---ThreadLocal(线程本地变量、线程本地存储)