#include <QtCore>
#include <iostream>
const int DataSize = 20;
const int BufferSize = 10;
char buffer[BufferSize];
{
public:
void run();
};
void Producer::run()
{
for (int i = 0; i < DataSize; ++i) {
freeSpace.acquire();
buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
std::cerr <<"Producer: buffer["<<i % BufferSize<<"]"<<buffer[i % BufferSize]<<std::endl;
usedSpace.release();
}
}
{
public:
void run();
};
void Consumer::run()
{
for (int i = 0; i < DataSize; ++i) {
usedSpace.acquire();
buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
std::cerr <<"Consumer: buffer["<<i % BufferSize<<"]"<<buffer[i % BufferSize]<<std::endl;
freeSpace.release();
}
//std::cerr << std::endl;
}
int main()
{
Producer producer;
Consumer consumer;
producer.start();
consumer.start();
producer.wait();
consumer.wait();
return 0;
}
//pro file
TEMPLATE = app
CONFIG += console thread
CONFIG -= app_bundle
SOURCES = semaphores.cpp
// one output is showed here
Producer: buffer[0]C
Producer: buffer[Consumer: buffer[01]]CT
Producer: buffer[Consumer: buffer[12]G]T
Producer: buffer[3]Consumer: buffer[2A
]G
Producer: buffer[4Consumer: buffer[3]C
]AProducer: buffer[
5]Consumer: buffer[4A
]C
Producer: buffer[6Consumer: buffer[]G5]
A
Producer: buffer[7Consumer: buffer[6]G
]G
Producer: buffer[8Consumer: buffer[]G7]
G
Producer: buffer[Consumer: buffer[89]A
]GProducer: buffer[0
Consumer: buffer[]C
9]Producer: buffer[A1]C
Consumer: buffer[
0]Producer: buffer[2C
]C
Consumer: buffer[1Producer: buffer[]3C]T
Consumer: buffer[2Producer: buffer[4]C]C
#include <QtCore>
#include <iostream>
const int DataSize = 20;
const int BufferSize = 10;
char buffer[BufferSize];
QSemaphore freeSpace(BufferSize); // for producer
QSemaphore usedSpace(0); // for consumer
class Producer : public QThread
{
public:
void run();
};
void Producer::run()
{
for (int i = 0; i < DataSize; ++i) {
freeSpace.acquire();
buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
std::cerr <<"Producer: buffer["<<i % BufferSize<<"]"<<buffer[i % BufferSize]<<std::endl;
usedSpace.release();
}
}
class Consumer : public QThread
{
public:
void run();
};
void Consumer::run()
{
for (int i = 0; i < DataSize; ++i) {
usedSpace.acquire();
buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
std::cerr <<"Consumer: buffer["<<i % BufferSize<<"]"<<buffer[i % BufferSize]<<std::endl;
freeSpace.release();
}
//std::cerr << std::endl;
}
int main()
{
Producer producer;
Consumer consumer;
producer.start();
consumer.start();
producer.wait();
consumer.wait();
return 0;
}
//pro file
TEMPLATE = app
CONFIG += console thread
CONFIG -= app_bundle
SOURCES = semaphores.cpp
// one output is showed here
Producer: buffer[0]C
Producer: buffer[Consumer: buffer[01]]CT
Producer: buffer[Consumer: buffer[12]G]T
Producer: buffer[3]Consumer: buffer[2A
]G
Producer: buffer[4Consumer: buffer[3]C
]AProducer: buffer[
5]Consumer: buffer[4A
]C
Producer: buffer[6Consumer: buffer[]G5]
A
Producer: buffer[7Consumer: buffer[6]G
]G
Producer: buffer[8Consumer: buffer[]G7]
G
Producer: buffer[Consumer: buffer[89]A
]GProducer: buffer[0
Consumer: buffer[]C
9]Producer: buffer[A1]C
Consumer: buffer[
0]Producer: buffer[2C
]C
Consumer: buffer[1Producer: buffer[]3C]T
Consumer: buffer[2Producer: buffer[4]C]C
To copy to clipboard, switch view to plain text mode
In this example,why would comes the output like this: Producer: buffer[Consumer: buffer[01]]CT. Isn't it should be all like this:Producer: buffer[0]C
Thanks!
Bookmarks