IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Thread ; Mutex; Producteurs/Consomateurs -> Problemes de deadlocks


Sujet :

C++

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2015
    Messages
    21
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 27
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2015
    Messages : 21
    Points : 22
    Points
    22
    Par défaut Thread ; Mutex; Producteurs/Consomateurs -> Problemes de deadlocks
    Bonjour !

    J'essayais tranquillement d'implementer un systeme de producteur consomateur quand je suis tombé sur un probleme de deadlocks.

    J'usque la impossible de trouver ce qui cloche dans mon code :/

    je vous met ici mon code et une partie de mes .hh , Si quelqu'un peut m'expliquer ce que je fait de mal je suis preneur.
    Pour ma part je pense que cela peut venir de mon std::mutex transformé en std:unique_lock<std::mutex>.

    main.cpp:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
     
    #include <unistd.h>
    #include <iostream>
    #include <vector>
    //#include <cpthread>                                                           
    #include <thread>
    #include <mutex>
    #include <condition_variable>
     
    #include "Thread.hh"
    #include "Mutex.hh"
    #include "Mutex_scoped.hh"
    #include "IMutex.hh"
    #include "ISafeQueue.hh"
    #include "Queue.hh"
    #include "Condvar.hh"
     
    //#define NB_THREAD     (3)                                                     
    //#define NB_INC                (100)                                           
    #define NB_PROD (2)
    #define NB_CONS (2)
     
     
    //Mutex mtx;                                                                    
    //int   count;                                                                  
    Queue   my_stack;
    std::mutex      mtx_cout;
     
    void    producer(int id, int cycle, Mutex *arg, Condvar *my_cond)
    {
      Mutex_scoped  mtx(arg);
     
      mtx_cout.lock();
      mtx_cout.unlock();
      while (cycle != 0)
        {
          mtx.trylock();
          my_stack.push(cycle);
          mtx_cout.lock();
          mtx_cout.unlock();
          mtx.unlock();
          my_cond->ready = true;
          mtx_cout.lock();
          mtx_cout.unlock();
          my_cond->signal();
          mtx_cout.lock();
          std::cout << "The producer n°" << id << "produce one ressource size " << \
    cycle  << " and signal he have a production" << std::endl;
          mtx_cout.unlock();
          cycle -= 1;
          sleep(0.5);
        }
      mtx_cout.lock();
      std::cout << "The producer n°" << id << "have finish this work" << std::endl;
      mtx_cout.unlock();
    }
     
     
    void    consumer(int id, Mutex *mtx, Condvar *my_cond)
    {
      int   value;
     
      std::cout << "CONSUUUMEEER" << std::endl;
      while ((my_cond->pop()) == true)
        {
          mtx->unique_lock();
          if ((my_stack.isEmpty()) == true)
            {
              mtx_cout.lock();
              std::cout << "The consumer n°" << id << "say: I have nothing to can c\
    onsume" << std::endl;
              mtx_cout.unlock();
              my_cond->wait();
              //mtx->unlock();                                                      
              mtx->unique_unlock();
            }
          else
            {
              my_stack.tryPop(&value);
              mtx_cout.lock();
              std::cout << "The consumer n°" << id << "consume one stack size of " \
    << value << std::endl;
              mtx_cout.unlock();
              if ((my_stack.isEmpty()) == false)
                {
                  mtx_cout.lock();
                  //mtx->unlock();                                                  
                  mtx->unique_unlock();
                  my_cond->signal();
                }
              else
                {
                  mtx->unique_unlock();
                  //mtx->unlock();                                                  
                }
              sleep(1);
            }
        }
      mtx_cout.lock();
      std::cout << "The consumer n°" << id << ": I have finish my work. Ciao !" << \
    std::endl;
      mtx_cout.unlock();
      mtx->unique_unlock();
    }
     
    Condvar::Condvar(Mutex *mtx, Queue *stack)
    {
      this->mtx = mtx;
      this->stack = stack;
      this->ready = false;
    }
     
    Condvar::~Condvar()
    {
    }
     
    void    Condvar::wait()
    {
     this->cond_var.wait(this->mtx->get_ulmtx());
    }
     
    void    Condvar::signal()
    {
      this->cond_var.notify_one();
    }
     
    void    Condvar::broadcast()
    {
      this->cond_var.notify_all();
    }
     
    bool   Condvar::pop()
    {
      if ((this->stack->isFinished()) == true)
        return (false);
      return (true);
    }
     
     
    int     main(int argc, char **argv)
    {
      //std::mutex  mtx;                                                            
      Mutex         mtx;
      Condvar       my_cond(&mtx, &my_stack);
      //Thread      th;                                                             
      std::vector<Thread *> thread_prod;
      std::vector<Thread *> thread_cons;
      unsigned int  i;
     
      for (i = 0; i != NB_PROD; ++i)
        {
          thread_prod.push_back(new Thread);
          thread_prod[i]->start_producer((int)i, 3, &mtx, &my_cond);
        }
      for (i = 0; i != NB_PROD; ++i)
        thread_prod[i]->join();
     
      my_stack.setFinished();
     
        for (i = 0; i != NB_CONS; ++i)
        {
          thread_cons.push_back(new Thread);
          thread_cons[i]->start_consumer((int)i, &mtx, &my_cond);
        }
      for (i = 0; i != NB_CONS; ++i)
      thread_cons[i]->join();
    }
    Mutex.hh
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
     
    class Mutex : IMutex
    {
      std::mutex    mtx;
      std::unique_lock<std::mutex>  ul_mtx;
     
    public:
      Mutex();
      virtual ~Mutex(void);
      virtual void lock(void);
      virtual void unlock(void);
      virtual bool trylock(void);
      void  unique_lock();
      void  unique_unlock();
      void  unique_trylock();
     
      std::unique_lock<std::mutex>  &get_ulmtx();
    };
    Mutex.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    utex::Mutex()
    {
      std::cout << "Creation of a new Mutex" << std::endl;
      this->ul_mtx = std::unique_lock<std::mutex>(this->mtx, std::defer_lock);
    }
     
    Mutex::~Mutex()
    {
      std::cout << "Destruction of Mutex" << std::endl;
    }
     
    void    Mutex::lock()
    {
      this->mtx.lock();
    }
     
    void    Mutex::unlock()
    {
      this->mtx.try_lock();
      this->mtx.unlock();
    }
     
    bool    Mutex::trylock()
    {
      if ((this->mtx.try_lock()) == true)
        return (true);
      return (false);
    }
     
    void    Mutex::unique_lock()
    {
      this->ul_mtx.lock();
    }
     
    void    Mutex::unique_unlock()
    {
      this->ul_mtx.unlock();
    }
     
    void    Mutex::unique_trylock()
    {
      this->ul_mtx.try_lock();
    }
     
    std::unique_lock<std::mutex>    &Mutex::get_ulmtx()
    {
      return (this->ul_mtx);
    }
    Bonne journéee

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 115
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 115
    Points : 32 967
    Points
    32 967
    Billets dans le blog
    4
    Par défaut
    Salut,

    honnêtement, j'ai pas tout lu. Ca part dans tous les sens, tu lock/unlock toutes les 2 lignes, parfois sans réaliser aucune action entre temps, avec 2 mutex différents pour je ne sais quelle raison, tu fais même appel à un try_lock dans la fonction unlock ?!?
    Partant de là, puisque c'est clairement n'importe quoi, je t'invite surtout à (re)lire comment marche un mutex et à quoi ça sert.
    1 appel à lock, 1 appel à unlock, et pas de fantaisie autour et surtout sans partir dans tous les sens à appeler lock/unlock/trylock au petit bonheur la chance sans qu'on y voie aucune logique.
    Et si tu penses avoir un deadlock, il suffit de mettre en pause le debugger et regarder où se trouvent les différents threads.
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

Discussions similaires

  1. Interface graphique et thread producteur-consomateur
    Par fockbliss17 dans le forum C#
    Réponses: 6
    Dernier message: 11/12/2010, 14h10
  2. Réponses: 3
    Dernier message: 18/04/2007, 14h02
  3. Thread, Mutex et bug ^^
    Par Zenol dans le forum Threads & Processus
    Réponses: 6
    Dernier message: 13/04/2007, 12h33
  4. threads & mutex
    Par keni dans le forum C
    Réponses: 3
    Dernier message: 23/02/2007, 16h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo