aca-tasks/task3/main.cpp
2023-12-02 14:34:06 +01:00

91 lines
2.1 KiB
C++

#include <iostream>
#include <QSemaphore>
#include <QThread>
#include <QCoreApplication>
/*
* Barlas Chpt 3 Ex 12:
Create three threads, each printing out the letters A, B, and C.
The printing must adhere to these rules:
• The total number of Bs and Cs that have been output at any
point in the output string cannot exceed the total number
of As that have been output at that point.
• After a C has been output, another C cannot be output until
one or more Bs have been output.
Use semaphores to solve the problem.
*/
class Printer {
private:
QSemaphore &lockBC;
QSemaphore &lockC;
public:
explicit Printer(QSemaphore &lockBC, QSemaphore &lockC) : lockBC(lockBC), lockC(lockC) {};
void printA() {
this->lockBC.release(1);
std::cout << "A\n";
}
void printB() {
if (this->lockBC.tryAcquire(1, 10000)) {
if (this->lockC.available() == 0) {
this->lockC.release(1);
}
std::cout << "B\n";
}
}
void printC() {
if (this->lockBC.tryAcquire(1, 10000)) {
if (this->lockC.tryAcquire(1, 10000)) {
std::cout << "C\n";
} else {
this->lockBC.release(1);
}
}
}
};
class Worker : public QThread {
private:
QSemaphore &lockBC;
QSemaphore &lockC;
public:
Worker(QSemaphore &lockBC, QSemaphore &lockC) : lockBC(lockBC), lockC(lockC) {};
protected:
void run() {
Printer print = Printer(this->lockBC, this->lockC);
print.printA();
print.printB();
print.printC();
}
};
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QSemaphore lockC(1);
QSemaphore lockBC(0);
Worker trd1(lockBC, lockC);
Worker trd2(lockBC, lockC);
Worker trd3(lockBC, lockC);
trd1.start();
trd2.start();
trd3.start();
// int N = 3;
// Worker *workers[N];
// for (int i = 0; i < N; i++) {
// workers[i] = new Worker(lockBC, lockC);
// workers[i]->run();
// }
return app.exec();
}