3

i would like to create multiple threads and launch it at the same time they are created, for the code to goes the fastest possible i tried to do this :

for (i = 1; i < nbClients+1; i++)
{
    pthread_create(&tClient, NULL, procedureClient, &i);
    pthread_join(tClient, NULL);

    free(clients[i].panier); //Libération de la mémoire pour chaque panier
}

if i do pthread_join, the main thread is suspended so it's sequential, so if i do this :

for (i = 1; i < nbClients+1; i++)
{
    pthread_create(&tClient, NULL, procedureClient, &i);

    free(clients[i].panier); //Libération de la mémoire pour chaque panier
}

is this execution parallel ? is it normal if my printf are in a disorder ? (i think that this is normal but i prefer to ask)

Thanks

Henley n
  • 3,593
  • 2
  • 10
  • 13
  • oh sorry dude i forgot to take off a line ! it's edited – Henley n Nov 27 '16 at 14:55
  • You are doing the join on the first thread before you create the second thread. That may be the fastest because it prevents the threads from running at the same time on different cores, which is better for cache. – stark Nov 27 '16 at 14:56
  • yes yes maybe but i have an assignement where my code has to be parallel programming, so threads HAS to be running at the same time, isn't it ? if i use phthread_join, the code cannot be running in parallel anymore isn't it ? – Henley n Nov 27 '16 at 15:00

1 Answers1

2

There are following issues.

  1. You need to have different pthread_t handles for different threads.
  2. Need different arg variables for different threads.
  3. You should call pthread_join() after creating all threads. pthread_join() will block the calling thread until the other thread exit. This will wait for that thread to exit.

.

pthread_t tClient[MAX_NUM_THREADS];
int       arg[MAX_NUM_THREADS] = {0};

for (i = 0; i < nbClients; i++)
{
    arg[i] = i+1; //Changed your for loop.
    pthread_create(&(tClient[i]), NULL, procedureClient, (void*)&(arg[i]));
}

/* Other code. */

for (i = 0; i < nbClients; i++)
{
    pthread_join(tClient[i], NULL); //Will wait for ith thread to exit.

    free(clients[i].panier); //Libération de la mémoire pour chaque panier
}

is this execution parallel ? is it normal if my printf are in a disorder ? (i think that this is normal but i prefer to ask)

Now the execution is parallel. Prints in different threads may come in different order. It depends on which thread gets scheduled at what time. You can synchronize the threads using mutex, semaphores, condition variables. etc.

MayurK
  • 1,925
  • 14
  • 27
  • yes i thought about trying this but if i use pthread_join, can the process still be parallel ?? – Henley n Nov 27 '16 at 15:11
  • 1
    @Henleyn Updated my answer. It has nothing to do with the execution of created threads. The thread which is calling pthread_join() will be blocked. – MayurK Nov 27 '16 at 15:15
  • okay no problem but why is useful for "arg", i don't understand ? – Henley n Nov 27 '16 at 15:46
  • 1
    As you know, the last argument of pthread_create() is a void pointer and it is passed to the thread_function. You are passing address of i to all threads and i is varying in for loop. So it may happen that before the thread_function read the value of i and store it locally (I assume you are doing it), for loop might be changing it. So it is better to pass different address for different threads. – MayurK Nov 27 '16 at 16:52