aca-tasks/task3/main.cpp

84 lines
2.1 KiB
C++
Raw Normal View History

2023-12-02 13:34:06 +00:00
#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:
2023-12-02 13:55:13 +00:00
QSemaphore &lockBC_;
QSemaphore &lockC_;
QString name_;
2023-12-02 13:34:06 +00:00
public:
2023-12-02 13:55:13 +00:00
explicit Printer(QSemaphore &lockBC, QSemaphore &lockC, QString name) : lockBC_(lockBC), lockC_(lockC),
name_(name) {};
2023-12-02 13:34:06 +00:00
void printA() {
2023-12-02 13:55:13 +00:00
this->lockBC_.release(1);
std::cout << name_.toStdString() << " A\n";
2023-12-02 13:34:06 +00:00
}
void printB() {
2023-12-02 13:55:13 +00:00
if (this->lockBC_.tryAcquire(1, 10000)) {
if (this->lockC_.available() == 0) {
this->lockC_.release(1);
2023-12-02 13:34:06 +00:00
}
2023-12-02 13:55:13 +00:00
std::cout << name_.toStdString() << " B\n";
2023-12-02 13:34:06 +00:00
}
}
void printC() {
2023-12-02 13:55:13 +00:00
if (this->lockBC_.tryAcquire(1, 10000)) {
if (this->lockC_.tryAcquire(1, 10000)) {
std::cout << name_.toStdString() << " C\n";
2023-12-02 13:34:06 +00:00
} else {
2023-12-02 13:55:13 +00:00
this->lockBC_.release(1);
2023-12-02 13:34:06 +00:00
}
}
}
};
class Worker : public QThread {
private:
2023-12-02 13:55:13 +00:00
Printer printer_;
2023-12-02 13:34:06 +00:00
public:
2023-12-02 13:55:13 +00:00
Worker(QSemaphore &lockBC, QSemaphore &lockC, QString tname) : printer_(lockBC, lockC, tname) {};
2023-12-02 13:34:06 +00:00
protected:
void run() {
2023-12-02 13:55:13 +00:00
printer_.printA();
printer_.printB();
printer_.printC();
2023-12-02 13:34:06 +00:00
}
};
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QSemaphore lockC(1);
QSemaphore lockBC(0);
2023-12-02 13:55:13 +00:00
int N = 3;
Worker *workers[N];
for (int i = 0; i < N; i++) {
workers[i] = new Worker(lockBC, lockC, QString("Thread %1").arg(i));
workers[i]->start();
}
2023-12-02 13:34:06 +00:00
return app.exec();
}