我如何创建一个全局变量是线程特定使用POSIX线程C 2 [英] How do I create a global variable that is thread-specific in C using POSIX threads?
问题描述
我正在学习有关POSIX线程和我来对线程特定数据部分。这本书并使用文件描述符一个很好的例子。然而,我想做我自己同样的例子,除了使用全局变量这个时候。不过,我有一些困难,充分抓住了这个概念。
I am learning about POSIX threads and I have come to the section on Thread Specific Data. The book does an excellent example using a file descriptor. However, I wanted to do the same example on my own, except this time using a global variable. However, I am having some difficulty fully grasping this concept.
我想要做的是以下内容:
What I want to do is the following:
- 创建一个全球性的整数
- 定义为全球int类型的键
在主:
- 设置全局整型是一个值,例如10
- 创建它的关键,没有任何清理
- 创建4个线程,并送他们来执行thread_func
- 检查是否值仍是10,因为线程只看到它的副本
在thread_func:
in thread_func:
- 使用pthread_setspecific(键,全局变量)来创建一个本地实例 - 不知道我这是正确的跨preting
- 调用一个函数 - DoSomething的()
- 退出
在do_something
in do_something
- 创建一个本地指针,并将其分配给pthread_getspecific(键) - 这应该让我的全局变量的线程特定版本
- 什么是存储在本地指针的值更改为2
- 退出
这里的code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMTHREADS 4
pthread_key_t glob_var_key;
int glob_var;
void do_something()
{
//get thread specific data
int* glob_spec_var = (int*) pthread_getspecific(glob_var_key);
printf("Thread %d glob_spec before mod value is %d\n", (unsigned int) pthread_self(), *glob_spec_var);
*glob_spec_var = 2;
printf("Thread %d glob_spec after mod value is %d\n", (unsigned int) pthread_self(), *glob_spec_var);
}
void* thread_func(void *arg)
{
pthread_setspecific(glob_var_key, &glob_var);
do_something();
pthread_exit(NULL);
}
int main(void)
{
pthread_t threads[NUMTHREADS];
int i;
glob_var = 10;
pthread_key_create(&glob_var_key,NULL);
printf("Main: glob_var is %d\n", glob_var);
for (i=0; i < NUMTHREADS; i++)
{
pthread_create(&threads[i],NULL,thread_func,NULL);
}
for (i=0; i < NUMTHREADS; i++)
{
pthread_join(threads[i], NULL);
}
printf("Main: glob_var is %d\n", glob_var);
return 0;
}
这是我的理解,当你调用pthread_getspecific,每个线程都应该有自己独特的内存地址的内存地址 - 我不觉得是这里的情况。我知道我不会这个正确的时候我也尝试做getspecific时要看每个线程的内存地址,我看到了相同的内存地址。也许有人可以点我到他们使用全局变量(不文件描述符),他们有特定线程的使用,其中螺纹看它作为一个局部变量的例子。
From what I understood, when you call pthread_getspecific, each thread is supposed to have its own unique memory address for the memory addresses - which I did not find to be the case here. I know I am not going about this correctly and when I did try to look at the memory addresses for each thread when doing getspecific, I saw the same memory address. Perhaps someone can point me to an example where they use a global variable (not file descriptors) and they have thread-specific usage in which threads look at it as a local variable.
推荐答案
TLS(线程本地存储)的根本目的是要提供一种定义机制,code可以检索存储在访问一个数据库线程特定数据一个全线程知名的共享密钥。您code为存储的相同的TLS中的数据(一个全局变量的地址)。因此,当一个线程请求该数据使用TLS的关键,他们都将取回的相同的地址。
The fundamental purpose of TLS (thread local storage) is to provide an defined mechanism whereby code can retrieve thread-specific data stored in a database accessed by a all-threads-known shared key. Your code is storing the same data in TLS (the address of a single global variable). Therefore when a thread requests this data using the tls-key, they will all get back the same address.
我想你打算你的code到做这样的事情:
I think you intend your code to do something like this:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUMTHREADS 4
pthread_key_t glob_var_key;
void do_something()
{
//get thread specific data
int* glob_spec_var = (int*) pthread_getspecific(glob_var_key);
printf("Thread %d before mod value is %d\n", (unsigned int) pthread_self(), *glob_spec_var);
*glob_spec_var += 1;
printf("Thread %d after mod value is %d\n", (unsigned int) pthread_self(), *glob_spec_var);
}
void* thread_func(void *arg)
{
int *p = malloc(sizeof(int));
*p = 1;
pthread_setspecific(glob_var_key, p);
do_something();
do_something();
pthread_setspecific(glob_var_key, NULL);
free(p);
pthread_exit(NULL);
}
int main(void)
{
pthread_t threads[NUMTHREADS];
int i;
pthread_key_create(&glob_var_key,NULL);
for (i=0; i < NUMTHREADS; i++)
pthread_create(&threads[i],NULL,thread_func,NULL);
for (i=0; i < NUMTHREADS; i++)
pthread_join(threads[i], NULL);
return 0;
}
输出
Thread 2625536 before mod value is 1
Thread 741376 before mod value is 1
Thread 3162112 before mod value is 1
Thread 3698688 before mod value is 1
Thread 2625536 after mod value is 2
Thread 741376 after mod value is 2
Thread 3162112 after mod value is 2
Thread 3698688 after mod value is 2
Thread 2625536 before mod value is 2
Thread 741376 before mod value is 2
Thread 3162112 before mod value is 2
Thread 3698688 before mod value is 2
Thread 2625536 after mod value is 3
Thread 741376 after mod value is 3
Thread 3162112 after mod value is 3
Thread 3698688 after mod value is 3
这篇关于我如何创建一个全局变量是线程特定使用POSIX线程C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!