I have a program that takes the argument then dynamically allocating space for multiple `pthread_t`(array of pthread), then `pthread_create()` to load function.
The problem is that my first thread just doesn't stop running. My second thread will not even create before my first thread finish the whole process. How do I solve this problem?
Here is a simple code to demonstrate my problem

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

pthread_t *ptr;
int num;

void* func(int id)
{
    while(1)
        printf("%d\n", id);
}

int main(int argc, char *argv[])
{
    int i;

    num = atoi(argv[1]); //assuming arguments are always valid

    ptr = malloc(sizeof(pthread_t)*num);

    for(i = 0; i < num; i++)
        pthread_create(&ptr[i], NULL, func(i), NULL);

    //free allocated space

    return 0;
}

```

My problem is that `0` is always printed, I never see `1`.
My guess is that ptr[1] never get the chance to initialize itself, so the thread doesn't exists. So it will not get it's cpu share.
Isn't the program itself has 1 main thread. After some process time, cpu should switch back to the main thread then pthread\_create the second thread.It never happens. I'm just wondering why.

I'm a newbie in C, and this is my first time doing pthread. So please give advice that suits my level. Thank you.

Thanks to all the great advices, I have changed my code to:

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

int num;
pthread_t *ptr;

void* func(void* id)
{
  int *c;
  c = (int*)id;
  while(1)
    if(c)
      printf("%d\n", *c);
}

int main(int argc, char *argv[])
{
  int i;

  num = atoi(argv[1]);

  ptr = malloc(sizeof(pthread_t)*num);
  for(i = 0; i < num; i++)
    pthread_create(&ptr[i], NULL, func, (void*)&i);

  for(i = 0; i < num; i++)
    pthread_join(ptr[i], NULL);

  return 0;
}

```

However, I still only see `0`. I created the thread now right?