C语言 pthread_t 指针作为 pthread_create 的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13956285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:48:08  来源:igfitidea点击:

pthread_t pointer as argument of pthread_create

cpointerspthreads

提问by user454322

The first argument of pthread_create is a pthread_t pointer. In the hello programbelow, if the first argument is a pointer to pthread_t (pthread_t*) instead of a pthread_t (pthread_t) the program ends with Segmentation fault...why?

pthread_create 的第一个参数是 pthread_t 指针。在下面的hello 程序中,如果第一个参数是指向 pthread_t ( pthread_t*) 而不是pthread_t ( )的指针,pthread_t则程序以Segmentation fault...为什么?

I don't remember seeing pthread_t*as the declared type of the first argument of pthread_create.
And chapter 2 of Butenhof's book Programming with POSIX Threadssays:

我不记得看到pthread_t*作为申报类型的第一个参数pthread_create
Butenhof 的书Programming with POSIX Threads 的第 2 章说:

To create a thread, you must declare a variable of type pthread_t[not pthread_t*].

要创建线程,您必须声明一个类型为pthread_t[not pthread_t*]的变量。

But according to the specificationthe first argument of pthread_createis a pointer to pthread_t, so why the segmentation fault?

但是根据规范,第一个参数pthread_create是指向 的指针pthread_t,那么为什么会出现分段错误呢?





分段故障

pthread_t* thr;
pthread_create(thr, NULL, &hello, NULL);





运行正常

pthread_t thr;
pthread_t* pntr = &thr;
pthread_create(pntr, NULL, &hello, NULL);





你好程序:

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

void * 
hello(void *arg){
  printf("Hello\n");
  pthread_exit(NULL);
}

int 
main(int argc, char **argv){
  pthread_t thr = 1;
  pthread_create(&thr, NULL, &hello, NULL);



  pthread_join(thr, NULL);

  return 0;
}


pthread_create prototype:

pthread_create 原型:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);

回答by simonc

pthread_t* thr;
pthread_create(thr, NULL, &hello, NULL);

declares a pointer to a pthread_twithout allocating storage for it. When you call pthread_create, it'll try writing to *thr. This is at an undefined location and will almost certainly fail.

声明一个指向 a 的指针,pthread_t而不为其分配存储空间。当您调用 时pthread_create,它会尝试写入*thr。这是在一个未定义的位置,几乎肯定会失败。

pthread_t thr;
pthread_t* pntr = &thr;
pthread_create(pntr, NULL, &hello, NULL);

works because you've declare storage (thron the stack) for a pthread_t.

之所以有效,是因为您已经thrpthread_t.

Note that the second, working, version can be simplified to what is used in your hello program

请注意,第二个工作版本可以简化为您的 hello 程序中使用的版本

pthread_t thr;
pthread_create(&thr, NULL, &hello, NULL);

...which declares a pthread_ton the stack then passes a pointer to it into pthread_create.

...pthread_t在堆栈上声明 a然后将指向它的指针传递给pthread_create.

回答by simonc

It is because if you simply declare a pointer, you can't expect it to point to allocated, initialised memory.

这是因为如果你简单地声明一个指针,你不能指望它指向分配的、初始化的内存。

When you instead declare a pthread_tit gets allocated its own little block of memory that you can then get the address to with the &operator and pass it to pthread_create.

当您改为声明 a 时,pthread_t它会分配自己的小内存块,然后您可以使用&运算符获取地址并将其传递给pthread_create.

回答by Mehdi

If you want to create some thread you can use below code:

如果你想创建一些线程,你可以使用下面的代码:

pthread_t* thread_handles;
thread_handles = malloc(thread_count * sizeof(pthread_t));
for (thread = 0; thread < thread_count; thread++)
{
  pthread_create(&thread_handles[thread], NULL, threadFunc, (void*)input);
}

In this way you should allocate memory for your handles before calling pthread_create, just like when you want to create some "struct" you should first allocate memory for them.

这样你应该在调用之前为你的句柄分配内存pthread_create,就像当你想创建一些“结构”时你应该首先为它们分配内存。