user454322
user454322

Reputation: 7649

pthread_t pointer as argument of pthread_create

The first argument of pthread_create is a pthread_t pointer. In the hello program below, 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?

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 Threads says:

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

But according to the specification the first argument of pthread_create is a pointer to pthread_t, so why the segmentation fault?



Segmentation fault

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



Runs OK

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



hello program:

#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:

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

Upvotes: 3

Views: 26323

Answers (3)

Mehdi
Mehdi

Reputation: 1

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.

Upvotes: 0

user308323
user308323

Reputation:

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_t it 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.

Upvotes: 4

simonc
simonc

Reputation: 42165

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

declares a pointer to a pthread_t without 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.

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

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

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

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

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

Upvotes: 9

Related Questions