Create a dynamic number of threads

Yes, but I would do the following:

  1. validate that argc > 1 before calling atoi(argv[1])

  2. validate numberOfThreads is a positive number and less than a reasonable range. (In case the user types 1000000).

  3. validate the return value from malloc is not null.

  4. pthread_create will not set errno on failure. So perror may not be the right function to call on failure.

...

if (argc > 1)
{
    int numberOfThreads = atoi(argv[1]); 
    if ((numberOfThreads <= 0) || (numberOfThreads > REASONABLE_THREAD_MAX))
    {
        printf("invalid argument for thread count\n");
        exit(EXIT_FAILURE);
    }
 
    thread = malloc(sizeof(pthread_t)*numberOfThreads); 
    if (thread == NULL)
    {
       printf("out of memory\n");
       exit(EXIT_FAILURE);
    }

    for (i = 0; i < numberOfThreads; i++)
    { 
        if (pthread_create ( &thread[i], NULL, &hilos_hijos, (void*) &info ) != 0)
        { 
            printf("Error al crear el hilo. \n"); 
            exit(EXIT_FAILURE); 
        }    
    }

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

void* thread_function(void)
{
    printf("hello");
}
int main(int argc,char *argv[])
{
    int noOfThread= atoi(argv[1]);
    pthread_t thread_id[noOfThread];
    int i;
    int status;
    for(i=0;i<noOfThread;i++)
    {
        pthread_create (&thread_id[i], NULL , &thread_function, NULL);
    }  

    for(i=0;i<noOfThread;i++)
        pthread_join(thread_id[i],NULL);   
}

Now compile thi and run as

./a.exe 3

So 3 thread will be created


In your code

1> why you are going to malloc ?

2> If malloc then why you are not going to free that ?