dimanche 1 mai 2016

[ Thread ]

Bonjour,

Une questions à propos du code ci-dessous :

Code:

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

struct params {
        pthread_mutex_t mutex;
        pthread_cond_t done;
        int id;
};

typedef struct params params_t;

void* hello(void* arg){

    int id;
    /* Lock.  */
    pthread_mutex_lock(&(*(params_t*)(arg)).mutex);

    /* Work.  */
    id = (*(params_t*)(arg)).id;
    printf("Hello from %d\n", id);

    /* Unlock and signal completion.  */
    pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
    pthread_cond_signal (&(*(params_t*)(arg)).done);

    /* After signalling `main`, the thread could actually
    go on to do more work in parallel.  */
}


int main() {

    pthread_t threads[10];
    params_t params;
    pthread_mutex_init (&params.mutex , NULL);
    pthread_cond_init (&params.done, NULL);

    /* Obtain a lock on the parameter.  */
    pthread_mutex_lock (&params.mutex);

    int i;
    for(i = 0; i < 10; i++) {

            /* Change the parameter (I own it).  */   
            params.id = i;

            /* Spawn a thread.  */
            pthread_create(&threads[i], NULL, hello, &params);

            /* Give up the lock, wait till thread is 'done',
            then reacquire the lock.  */
            pthread_cond_wait (&params.done, &params.mutex);
    }

    for(i = 0; i < 10; i++) {
            pthread_join(threads[i], NULL);
    }

    /* Destroy all synchronization primitives.  */   
    pthread_mutex_destroy (&params.mutex);
    pthread_cond_destroy (&params.done);

    return 0;
}

Le code n'est pas très compliqué, on cherche juste à incrémenter une variable dans des thread différents.

1 - Sachant que la boucle qui crée les thread est synchronisée avec la fin du thread créer précédemment, on aura toujours qu'un seul thread en exécution, et non pas les 10 en même temps ?

Code:

or(i = 0; i < 10; i++) {

            /* Change the parameter (I own it).  */   
            params.id = i;

            /* Spawn a thread.  */
            pthread_create(&threads[i], NULL, hello, &params);

            /* Give up the lock, wait till thread is 'done',
            then reacquire the lock.  */
            pthread_cond_wait (&params.done, &params.mutex);
    }

2 - La boucle de mise en attente de la fin des thread dans la fonction main semble inutile car nous avons déjà la fonction pthread_cond_wait() qui verrouille son exécution ?

Code:

for(i = 0; i < 10; i++) {
            pthread_join(threads[i], NULL);
    }

Aucuns intérêts d'utiliser des thread donc, j'ai compilé le programme sans la boucle pthread_join ci-dessus, j'ai bien ma suite de 0-9 dans l'ordre.

Cordialement,

Shirocen.


from Hackademics : Forum de hacking – hackers white hat – cours de securite informatique, apprendre langage python, tutoriels de reverse engineering http://ift.tt/1Wzu2YW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire