我最近一直在尝试线程本地存储.我有工作代码,每件事似乎都很好但是当我用valgrind运行我的程序时,看起来有一些问题.
我的问题是,如果我将内存分配给静态线程本地存储将在线程退出时被删除吗?
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
void *test (void *arg)
{
static __thread int val = 0;
static __thread char *string = NULL;
string = (char *) calloc (100, sizeof (char));
strcpy (string, "hello");
val++;
printf ("val(%p):%d\n", &val, val);
printf ("string(%p):%s\n", &string, string);
pthread_exit (NULL);
}
int main (int argc, char *argv[])
{
int num_threads = 10, i;
pthread_t tid[num_threads];
for (i=0;i<num_threads;i++) {
pthread_create (&tid[i], NULL, &test, NULL);
}
for (i=0;i<num_threads;i++) {
pthread_join (tid[i], NULL); …Run Code Online (Sandbox Code Playgroud)