#include <QtCore>
#include <iostream>
const int DataSize = 100000;
const int BufferSize = 4096;
char buffer[BufferSize];
QWaitCondition bufferIsNotFull;
QWaitCondition bufferIsNotEmpty;
QMutex mutex;
int usedSpace = 0;
class Producer : public QThread
{
public:
void run();
};
void Producer::run()
{
for (int i = 0; i < DataSize; ++i) {
mutex.lock();
while (usedSpace == BufferSize)
bufferIsNotFull.wait(&mutex);
buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
++usedSpace;
bufferIsNotEmpty.wakeAll();
mutex.unlock();
}
}
class Consumer : public QThread
{
public:
void run();
};
void Consumer::run()
{
for (int i = 0; i < DataSize; ++i) {
mutex.lock();
while (usedSpace == 0)
bufferIsNotEmpty.wait(&mutex);
std::cerr << buffer[i % BufferSize];
--usedSpace;
bufferIsNotFull.wakeAll();
mutex.unlock();
}
std::cerr << std::endl;
}
int main()
{
Producer producer;
Consumer consumer;
producer.start();
consumer.start();
producer.wait();
consumer.wait();
return 0;
}
template<typename Data>
class concurrent_queue
{
private:
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
public:
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
void wait_and_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
popped_value=the_queue.front();
the_queue.pop();
}
};
#include <mutex>
#include <condition_variable>
#include <deque>
#include <iostream>
using namespace std;
std::condition_variable QueueNotFull;
std::condition_variable QueueNotEmpty;
std::mutex QueueMutex;
std::deque<int> Que;
const int MaxSize = 16
bool Empty(){
return Que.empty();
}
bool Full(){
return Que.size() == MaxSize;
}
void Put(int element){
std::unique_lock<std::mutex> locker(QueueMutex);
while (Full()){
cout << "Full !! Wait !!" << endl;
QueueNotFull.wait(locker);
}
Que.push_back(element);
QueueNotEmpty.notify_all();
}
int Get(){
std::unique_lock<std::mutex> locker(QueueMutex);
while (Empty()){
cout << "Empty!! Wait!!" << endl;
QueueNotEmpty.wait(locker);
}
int t = Que.front();
Que.pop_front();
QueueNotFull.notify_all();
return t;
}
void GetFunc(){
while (1){
int var;
var = Get();
cout << "Get : " << var << endl;
}
}
void PutFunc(){
srand((unsigned int)time(0));
while (1){
int var = rand()%100;
Put(var);
}
}
int main(int arc,char** argv)
{
std::thread td1(GetFunc);
std::thread td2(PutFunc);
td1.join();
td2.join();
return 0;
}
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <deque>
#include <thread>
std::condition_variable notFull;
std::condition_variable notEmpty;
std::mutex mtx;
std::deque<int> deq;
const int MAX_SIZE = 16;
bool empty(){
return deq.empty();
}
bool full(){
return deq.size() == MAX_SIZE;
}
void put(int num){
std::unique_lock<std::mutex> lock(mtx);
while (full()){
notFull.wait(lock);
}
deq.push_back(num);
notEmpty.notify_all();
}
int get(){
std::unique_lock<std::mutex> lock(mtx);
while (empty()){
notEmpty.wait(lock);
}
int num = deq.front();
deq.pop_front();
notFull.notify_all();
return num;
}
void putFunc(){
int i = 0;
while (i < 100000){
put(i++);
}
}
void getFunc(){
int i = 0;
while (i++ < 100){
cout << get() << endl;
}
}
int main()
{
std::thread t1(putFunc);
std::thread t2(getFunc);
t1.join();
t2.join();
system("pause");
}