c++11 producer and customer design
4506 ワード
#include
#include
#include
#include
#include
#include
#include
std::mutex mut;
std::mutex ful;
std::queue data_queue;
std::condition_variable data_cond;
std::condition_variable data_full;
std::default_random_engine e(time(0));
std::uniform_int_distribution dis(1,5);
#define MAX_QUEUE_SIZE 10
int cur_num = 0;
void provider() {
while (true) {
if (cur_num < MAX_QUEUE_SIZE) {
int num = cur_num;
std::this_thread::sleep_for(std::chrono::seconds(dis(e)));
std::lock_guard<:mutex> lk(mut);
data_queue.push(num);
cur_num++;
std::cout << "produce next one: " << data_queue.size() << std::endl;
data_cond.notify_one();
} else {
std::unique_lock<:mutex> lk(ful);
data_full.wait(lk,[]{ return data_queue().size < MAX_QUEUE_SIZE ;});
std::cout << "it's time to produce next one: " << data_queue.size() << std::endl;
}
}
}
void consumer() {
while (true) {
std::unique_lock<:mutex> lk(mut);
data_cond.wait(lk,[]{ return !data_queue.empty(); });
int num = data_queue.front();
data_queue.pop();
cur_num--;
lk.unlock();
std::cout << "eat num: " << num << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(dis(e)));
std::lock_guard<:mutex> full_mutex(ful);
data_full.notify_one();
}
}
int main() {
std::thread t1(provider);
std::thread t2(consumer);
t1.join();
t2.join();
return 0;
}
modified it by RAII of C++, let's write another struct: threadsafe_queue demo:
#include
#include
#include
#include
#include
#include
template
class threadsafe_queue {
private:
mutable std::mutex mut;
std::queue data_queue;
std::condition_variable data_cond;
public:
threadsafe_queue() {}
threadsafe_queue(threadsafe_queue const& other) {
std::lock_guard<:mutex> lk(other.mut);
data_queue = other.data_queue;
}
void push(T new_value) {
std::lock_guard<:mutex> lk(mut);
data_queue.push(new_value);
data_cond.notify_one();
}
void wait_and_pop(T& value) {
std::unique_lock<:mutex> lk(mut);
data_cond.wait(lk, [this]{ return !data_queue.empty(); });
value = data_queue.front();
data_queue.pop();
}
std::shared_ptr wait_and_pop() {
std::unique_lock<:mutex> lk(mut);
data_cond.wait(lk, [this] { return !data_queue.empty(); });
std::shared_ptr res(std::make_shared(data_queue.front()));
data_queue.pop();
return res;
}
bool try_pop(T& value) {
std::lock_guard<:mutex> lk(mut);
if (data_queue.empty())
return false;
value = data_queue.front();
data_queue.pop();
return true;
}
std::shared_ptr try_pop() {
std::lock_guard<:mutex> lk(mut);
if (data_queue.empty())
return std::shared_ptr();
std::shared_ptr res(std::make_shared(data_queue.front()));
data_queue.pop();
return res;
}
bool empty() const {
std::lock_guard<:mutex> lk(mut);
return data_queue.empty();
}
};
threadsafe_queue queue;
int num;
void producer() {
while(true) {
std::cout << "producer: " << num << std::endl;
queue.push(++num);
}
}
void consumer() {
while (true) {
std::shared_ptr num = queue.wait_and_pop();
std::cout << "consumer: " << *num << std::endl;
}
}
int main() {
std::thread t1(producer);
std::thread t2(consumer);
t1.join();
t2.join();
return 0;
}