6

I want to create a number of threads specified by the user. The code I have written for this is:

int nhijos = atoi(argv[1]);

thread = malloc(sizeof(pthread_t)*nhijos);

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

Is this correct?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Alessandroempire
  • 1,640
  • 4
  • 31
  • 54
  • WHY? Why are you creating a number of threads defined by the user? Just get the number of threads that are required to get the job done. – Ed Heal Jun 22 '12 at 17:03
  • 2
    because the user is supposed to specify the number of concurrent threads... because thats how we are suppose to program this project... – Alessandroempire Jun 22 '12 at 17:06

2 Answers2

7

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); 
        }    
    }
Nemesius
  • 179
  • 10
selbie
  • 100,020
  • 15
  • 103
  • 173
4
#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 ?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • throws erros when compiling... i tried this before and it throws warnings, thats why i used malloc – Alessandroempire Jun 22 '12 at 17:13
  • padre.c:12:5: warning: ISO C90 forbids variable length array ‘thread_id’ padre.c:70:2: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type /usr/include/pthread.h:225:12: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **’ padre.c:80:2: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type /usr/include/pthread.h:225:12: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **’ padre.c:89:7: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast – Alessandroempire Jun 22 '12 at 17:16
  • oh in c90 i think VLA are not allowed in c99 these are allowed. thats why you get warning http://stackoverflow.com/questions/3082126/c99-faq-and-variable-length-arrays – Jeegar Patel Jun 22 '12 at 17:21
  • i see. well in that case thats valid. ill give it a try. – Alessandroempire Jun 22 '12 at 17:23