C 如何在线程中共享内存

C 如何在线程中共享内存,c,linux,multithreading,C,Linux,Multithreading,我试图在线程函数内使用我在主函数中分配的内存,但我得到了错误:未定义对“product\u line”的引用。 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include "test1.c" int main() { char** product_line; product_line = malloc(sizeof(char *)); product_li

我试图在线程函数内使用我在主函数中分配的内存,但我得到了错误:
未定义对“product\u line”的引用。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "test1.c"

int main()
{
    char** product_line;
    product_line = malloc(sizeof(char *));

    product_line[0] = "Toy";    
    product_line[1] = "Chair";
    product_line[2] = "Door";

    pthread_t thread_tid;
    pthread_create(&thread_tid, NULL, foo, NULL);

    pthread_join(thread_tid, NULL);

return 0;
}
#包括
#包括
#包括
#包括“test1.c”
int main()
{
字符**产品线;
产品线=malloc(sizeof(char*));
产品线[0]=“玩具”;
产品线[1]=“椅子”;
产品线[2]=“门”;
pthread\u t thread\u tid;
pthread_create(&thread_tid,NULL,foo,NULL);
pthread_join(thread_tid,NULL);
返回0;
}
这是线程调用的单独源:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

extern char** product_line;

void * foo(void *param)
{
    int i;
    printf("%s\n", product_line[0]);
    for (i = 0; i < 3; i++)
        printf("%s\n", product_line[i]);
}
#包括
#包括
#包括
外部字符**产品线;
void*foo(void*param)
{
int i;
printf(“%s\n”,产品线[0]);
对于(i=0;i<3;i++)
printf(“%s\n”,产品线[i]);
}

如何修复此错误?

变量
product\u line
应在
main
之外声明,否则无法从其他翻译单元访问:

/* main.c */
…

char **product_line;

int main()
{
    …
}

MikeCAT提到的另一个问题:


包含
.c
源文件是不寻常的

考虑添加头文件
test1.h

/* test1.h */
#ifndef TEST1_H
#define TEST1_H

void * foo(void *param);

#endif
然后,将
#在
main.c
中包含“test1.c”
替换为:

#include "test1.h"
当编译
main.c
test1.c
时,需要这样做以避免重复定义
foo
。(假设您使用类似于
cc main.c test1.c
的东西编译代码)建议(尽管是可选的)在
test1.c
中也包含
test1.h


马丁·詹姆斯还提到:


为一个指针分配空间,然后使用其中三个指针:((

这条线

product_line = malloc(sizeof(char *));
为单个
char*
指针分配空间。但是,实际上您正在使用其中的3个指针,因此需要分配3个
char*
指针:

product_line = malloc(sizeof(char *) * 3);

变量
product\u line
应在
main
之外声明,否则无法从其他翻译单元访问:

/* main.c */
…

char **product_line;

int main()
{
    …
}

MikeCAT提到的另一个问题:


包含
.c
源文件是不寻常的

考虑添加头文件
test1.h

/* test1.h */
#ifndef TEST1_H
#define TEST1_H

void * foo(void *param);

#endif
然后,将
#在
main.c
中包含“test1.c”
替换为:

#include "test1.h"
当编译
main.c
test1.c
时,这是为了避免重复定义
foo
。(假设您使用类似于
cc main.c test1.c
的东西编译代码),建议(尽管是可选的)在
test1.c
中也包含
test1.h


马丁·詹姆斯还提到:


为一个指针分配空间,然后使用其中三个指针:((

这条线

product_line = malloc(sizeof(char *));
为单个
char*
指针分配空间。但是,实际上您正在使用其中的3个指针,因此需要分配3个
char*
指针:

product_line = malloc(sizeof(char *) * 3);

产品线
的定义不可访问,因为它是局部变量,而不是全局变量

使
产品线
全球化是一种简单的解决方法

另一种解决方案是将
产品线
作为参数传递

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void * foo(void *param)
{
    char** product_line = param;
    int i;
    printf("%s\n", product_line[0]);
    for (i = 0; i < 3; i++)
        printf("%s\n", product_line[i]);
    return NULL;
}

int main(void)
{
    const char** product_line; /* strings literals are not modifyable */
    product_line = malloc(sizeof(const char *) * 3); /* Do allocate enough buffer! */
    if (product_line == NULL) /* You should check if malloc() was successful. */
    {
        perror("malloc");
        return 1;
    }
    product_line[0] = "Toy";
    product_line[1] = "Chair";
    product_line[2] = "Door";

    pthread_t thread_tid;
    pthread_create(&thread_tid, NULL, foo, product_line);

    pthread_join(thread_tid, NULL);

    free(product_line); /* You should free() what you allocatted via malloc(). */
    return 0;
}
#包括
#包括
#包括
void*foo(void*param)
{
字符**产品线=参数;
int i;
printf(“%s\n”,产品线[0]);
对于(i=0;i<3;i++)
printf(“%s\n”,产品线[i]);
返回NULL;
}
内部主(空)
{
常量字符**产品线;/*字符串文字不可修改*/
product_line=malloc(sizeof(const char*)*3);/*请分配足够的缓冲区*/
如果(product_line==NULL)/*您应该检查malloc()是否成功*/
{
佩罗尔(“马洛克”);
返回1;
}
产品线[0]=“玩具”;
产品线[1]=“椅子”;
产品线[2]=“门”;
pthread\u t thread\u tid;
pthread_create(&thread_tid,NULL,foo,product_line);
pthread_join(thread_tid,NULL);
free(product_line);/*您应该释放()通过malloc()分配的内容*/
返回0;
}

无法访问
产品线的定义,因为它是局部变量,而不是全局变量

使
产品线
全球化是一种简单的解决方法

另一种解决方案是将
产品线
作为参数传递

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void * foo(void *param)
{
    char** product_line = param;
    int i;
    printf("%s\n", product_line[0]);
    for (i = 0; i < 3; i++)
        printf("%s\n", product_line[i]);
    return NULL;
}

int main(void)
{
    const char** product_line; /* strings literals are not modifyable */
    product_line = malloc(sizeof(const char *) * 3); /* Do allocate enough buffer! */
    if (product_line == NULL) /* You should check if malloc() was successful. */
    {
        perror("malloc");
        return 1;
    }
    product_line[0] = "Toy";
    product_line[1] = "Chair";
    product_line[2] = "Door";

    pthread_t thread_tid;
    pthread_create(&thread_tid, NULL, foo, product_line);

    pthread_join(thread_tid, NULL);

    free(product_line); /* You should free() what you allocatted via malloc(). */
    return 0;
}
#包括
#包括
#包括
void*foo(void*param)
{
字符**产品线=参数;
int i;
printf(“%s\n”,产品线[0]);
对于(i=0;i<3;i++)
printf(“%s\n”,产品线[i]);
返回NULL;
}
内部主(空)
{
常量字符**产品线;/*字符串文字不可修改*/
product_line=malloc(sizeof(const char*)*3);/*请分配足够的缓冲区*/
如果(product_line==NULL)/*您应该检查malloc()是否成功*/
{
佩罗尔(“马洛克”);
返回1;
}
产品线[0]=“玩具”;
产品线[1]=“椅子”;
产品线[2]=“门”;
pthread\u t thread\u tid;
pthread_create(&thread_tid,NULL,foo,product_line);
pthread_join(thread_tid,NULL);
free(product_line);/*您应该释放()通过malloc()分配的内容*/
返回0;
}

您应该知道局部(自动)变量和全局变量之间的区别

以下链接中的变量范围:

如果您阅读了上一篇教程,您将能够自己检测错误。
product\u line
变量是自动的,这意味着一旦您进入“main”函数,它将在堆栈区域中创建,并通过退出该函数而销毁

因此,此变量不适用于
foo
函数;不在其堆栈上。 建议的解决办法:

  • 制作一个可用于所有函数的全局指针

  • foo
    函数添加新参数,并将此指针传递给它

注意:

包含.c文件是不常见的,而是包含有原型和d的.h文件