Compare commits
2 Commits
9c4dbf6dc6
...
04c37c5078
Author | SHA1 | Date | |
---|---|---|---|
04c37c5078 | |||
71fad746b3 |
@ -26,3 +26,4 @@ add_subdirectory(third-party/fmt)
|
||||
|
||||
# Include CMakeLists files from subdirs for specific tasks
|
||||
add_subdirectory(task1)
|
||||
add_subdirectory(task3)
|
@ -7,14 +7,24 @@
|
||||
},
|
||||
"configurePresets": [
|
||||
{
|
||||
"name": "task1@release",
|
||||
"displayName": "Task1 Release build",
|
||||
"description": "Builds the targets of task1 as release",
|
||||
"name": "task@release",
|
||||
"displayName": "Task Release build",
|
||||
"description": "Builds the target as release",
|
||||
"generator": "Ninja",
|
||||
"binaryDir": "${sourceDir}/.out/task1-release",
|
||||
"binaryDir": "${sourceDir}/.out/task-release",
|
||||
"cacheVariables": {
|
||||
"CMAKE_BUILD_TYPE": "Release"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "task@debug",
|
||||
"displayName": "Task Debug build",
|
||||
"description": "Builds the target as debug",
|
||||
"generator": "Ninja",
|
||||
"binaryDir": "${sourceDir}/.out/task-debug",
|
||||
"cacheVariables": {
|
||||
"CMAKE_BUILD_TYPE": "Debug"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
8
task3/CMakeLists.txt
Normal file
8
task3/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
find_package(Qt6 COMPONENTS Core REQUIRED)
|
||||
|
||||
add_executable(task3-chpt3ex12)
|
||||
target_sources(task3-chpt3ex12 PRIVATE
|
||||
main.cpp)
|
||||
target_link_libraries(task3-chpt3ex12 PRIVATE
|
||||
Qt6::Core)
|
83
task3/main.cpp
Normal file
83
task3/main.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#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_;
|
||||
|
||||
QString name_;
|
||||
|
||||
public:
|
||||
explicit Printer(QSemaphore &lockBC, QSemaphore &lockC, QString name) : lockBC_(lockBC), lockC_(lockC),
|
||||
name_(name) {};
|
||||
|
||||
void printA() {
|
||||
this->lockBC_.release(1);
|
||||
std::cout << name_.toStdString() << " A\n";
|
||||
}
|
||||
|
||||
void printB() {
|
||||
if (this->lockBC_.tryAcquire(1, 10000)) {
|
||||
if (this->lockC_.available() == 0) {
|
||||
this->lockC_.release(1);
|
||||
}
|
||||
std::cout << name_.toStdString() << " B\n";
|
||||
}
|
||||
}
|
||||
|
||||
void printC() {
|
||||
if (this->lockBC_.tryAcquire(1, 10000)) {
|
||||
if (this->lockC_.tryAcquire(1, 10000)) {
|
||||
std::cout << name_.toStdString() << " C\n";
|
||||
} else {
|
||||
this->lockBC_.release(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class Worker : public QThread {
|
||||
private:
|
||||
Printer printer_;
|
||||
|
||||
public:
|
||||
Worker(QSemaphore &lockBC, QSemaphore &lockC, QString tname) : printer_(lockBC, lockC, tname) {};
|
||||
|
||||
protected:
|
||||
void run() {
|
||||
printer_.printA();
|
||||
printer_.printB();
|
||||
printer_.printC();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
QCoreApplication app(argc, argv);
|
||||
QSemaphore lockC(1);
|
||||
QSemaphore lockBC(0);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return app.exec();
|
||||
}
|
Loading…
Reference in New Issue
Block a user