I'm looking at a Qt specific C++ solution for the typical producer/consumer problem. Here's the code for the producer:
class Producer : public QThread
{
public:
void run()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for (int i = 0; i < DataSize; ++i) {
freeBytes.acquire(); // (1)
buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; // (2)
usedBytes.release();
}
}
};
I am not able to understand the second line in the for
loop viz. "ACGT"[*]
syntax. What does it do exactly? Is this Qt specific or is this C++ syntax I'm not aware of?
PS: Full source code here
It generates a random characted from: A, C, G, T.
Literal "ACGT"
is an array of type char const [5]
then [(int)qrand() % 4]
is a random index in a range from 0 to 3 including.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With