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();
} |
Partager