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:
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:
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:
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