pthread_create()调用的函数有多个参数?

pthread_create()调用的函数有多个参数?,c,pthreads,C,Pthreads,我需要将多个参数传递给我希望在单独线程上调用的函数。我知道,实现这一点的典型方法是定义一个结构,向函数传递一个指向该结构的指针,然后为参数取消引用它。但是,我无法使其工作: #include <stdio.h> #include <pthread.h> struct arg_struct { int arg1; int arg2; }; void *print_the_arguments(void *arguments) { struct ar

我需要将多个参数传递给我希望在单独线程上调用的函数。我知道,实现这一点的典型方法是定义一个结构,向函数传递一个指向该结构的指针,然后为参数取消引用它。但是,我无法使其工作:

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

struct arg_struct {
    int arg1;
    int arg2;
};

void *print_the_arguments(void *arguments)
{
    struct arg_struct *args = (struct arg_struct *)args;
    printf("%d\n", args -> arg1);
    printf("%d\n", args -> arg2);
    pthread_exit(NULL);
    return NULL;
}

int main()
{
    pthread_t some_thread;
    struct arg_struct args;
    args.arg1 = 5;
    args.arg2 = 7;

    if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)&args) != 0) {
        printf("Uh-oh!\n");
        return -1;
    }

    return pthread_join(some_thread, NULL); /* Wait until thread is finished */
}
但当我运行它时,我实际上得到:

141921115
-1947974263
有人知道我做错了什么吗?

因为你说

struct arg_struct*args=(struct arg_struct*)args

而不是

struct arg\u struct*args=参数

main()
有自己的线程和堆栈变量。为堆中的“args”分配内存或使其成为全局内存:

struct arg_struct {
    int arg1;
    int arg2;
}args;

//declares args as global out of main()
当然,然后将引用从
args->arg1
更改为
args.arg1
等。

使用

struct arg_struct *args = (struct arg_struct *)arguments;
代替

struct arg_struct *args = (struct arg_struct *)args;
使用:

然后像这样传递这些参数:

pthread_create(&tr, NULL, print_the_arguments, (void *)args);

别忘了免费的args!;)

打印参数的参数是参数,因此您应该使用:

struct arg_struct *args = (struct arg_struct *)arguments. 
-->这个赋值是错误的,我的意思是变量参数应该在这个上下文中使用。
干杯

在此代码的线程创建中,传递函数指针的地址。 原著
pthread\u创建(&some\u thread,NULL,&print\u参数,(void*)&args)!=0

它应该读为
pthread\u创建(&some\u thread,NULL,print\u参数,(void*)&args)

记住的一个好方法是,这个函数的所有参数都应该是地址

某些线程
是静态声明的,因此使用
&
正确发送地址

我将创建一个
pthread\u attr\u t
变量,然后对其使用
pthread\u attr\u init()
,并传递该变量的地址。但是,传递
NULL
指针也是有效的

功能标签前面的
&
是导致此问题的原因。所使用的标签已经是函数的
void*
,因此只需要该标签

!=带有最终参数的0
似乎会导致不确定的行为。添加这意味着传递的是布尔值而不是引用


Akash Agrawal的回答也是该代码问题解决方案的一部分。

我有与原始海报相同的问题,Michael

但是,我尝试应用为原始代码提交的答案,但没有成功

经过一些尝试和错误之后,下面是我的代码版本,它可以运行(或者至少对我有效!)。如果仔细观察,您会发现它与先前发布的解决方案不同

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

struct arg_struct
{
   int arg1;
   int arg2;
} *args;

void *print_the_arguments(void *arguments)
{
   struct arg_struct *args = arguments;
   printf("Thread\n");
   printf("%d\n", args->arg1);
   printf("%d\n", args->arg2);
   pthread_exit(NULL);
   return NULL;
}

int main()
{
   pthread_t some_thread;
   args = malloc(sizeof(struct arg_struct) * 1);

   args->arg1 = 5;
   args->arg2 = 7;

   printf("Before\n");
   printf("%d\n", args->arg1);
   printf("%d\n", args->arg2);
   printf("\n");


   if (pthread_create(&some_thread, NULL, &print_the_arguments, args) != 0)
   {
      printf("Uh-oh!\n");
      return -1;
   }

   return pthread_join(some_thread, NULL); /* Wait until thread is finished */
}
#包括
#包括
#包括
结构arg_结构
{
int arg1;
int-arg2;
}*args;
void*打印参数(void*参数)
{
struct arg_struct*args=参数;
printf(“线程\n”);
printf(“%d\n”,args->arg1);
printf(“%d\n”,args->arg2);
pthread_exit(NULL);
返回NULL;
}
int main()
{
pthread\u t一些线程;
args=malloc(sizeof(struct arg_struct)*1);
args->arg1=5;
args->arg2=7;
printf(“之前”);
printf(“%d\n”,args->arg1);
printf(“%d\n”,args->arg2);
printf(“\n”);
if(pthread_create(&some_thread,NULL,&print_参数,args)!=0)
{
printf(“Uh-oh!\n”);
返回-1;
}
返回pthread_join(some_thread,NULL);/*等待线程完成*/
}
< /代码> 因为我最好还是提供C++代码作为答案。
//hello-2args.cpp
// https://stackoverflow.com/questions/1352749
#包括
#包括
#包括
使用名称空间std;
typedef结构线程参数{
int thrnr;
char*msg;
}塔尔塞特;
void*print\u hello(void*thrgs){

可以试着在堆上分配它吗?@Carson为什么会有不同?你的结构应该至少和你的线程一样长。如果你正在创建一个线程并从调用pthread_create()的函数返回,堆栈上分配的结构可能会被其他数据覆盖,并可能导致线程函数出现问题。在本例中,这不是问题,因为创建线程等待工作线程完成后再返回。@Commodore Jaeger噢!谢谢,这就是我与另一个正在处理的线程之间的问题。我正如Carson所说,通过使用malloc()在堆上分配它来修复它。这现在更有意义了。@sigjuice,它对我不起作用。我看到编译错误:从“void*”到“arg_struct*”的转换无效。
struct arg_struct *args = (struct arg_struct *)arguments. 
struct arg_struct *args = (struct arg_struct *)args;
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct arg_struct
{
   int arg1;
   int arg2;
} *args;

void *print_the_arguments(void *arguments)
{
   struct arg_struct *args = arguments;
   printf("Thread\n");
   printf("%d\n", args->arg1);
   printf("%d\n", args->arg2);
   pthread_exit(NULL);
   return NULL;
}

int main()
{
   pthread_t some_thread;
   args = malloc(sizeof(struct arg_struct) * 1);

   args->arg1 = 5;
   args->arg2 = 7;

   printf("Before\n");
   printf("%d\n", args->arg1);
   printf("%d\n", args->arg2);
   printf("\n");


   if (pthread_create(&some_thread, NULL, &print_the_arguments, args) != 0)
   {
      printf("Uh-oh!\n");
      return -1;
   }

   return pthread_join(some_thread, NULL); /* Wait until thread is finished */
}
g++ -fopenmp -pthread hello-2args.cpp && ./a.out # Linux
g++ -fopenmp -pthread hello-2args.cpp && ./a.exe # MSYS2, Windows