Compare commits

...

2 Commits

Author SHA1 Message Date
c92bff3c23 cleanup 2023-12-09 13:22:46 +01:00
a335828778 Fix crappy template 2023-12-07 21:39:28 +01:00

View File

@ -3,18 +3,6 @@
#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.
*/
struct IPrintable {
~IPrintable() = default;
@ -69,11 +57,12 @@ public:
};
template<typename T, typename ...Argts>
template<typename T>
class Worker : public QThread {
private:
std::unique_ptr<IPrintable> printer_;
public:
template<typename ...Argts>
Worker(QString tname, Argts &... args) : printer_(std::make_unique<T>(args...)) {};
protected:
@ -88,14 +77,13 @@ protected:
int main(int argc, char *argv[]) {
//QCoreApplication app(argc, argv);
QSemaphore lockC(1);
QSemaphore lockBC(0);
Worker<PrintA, QSemaphore> t1("Thread 1", lockBC);
Worker<PrintB, QSemaphore, QSemaphore> t2("Thread 1", lockBC, lockC);
Worker<PrintC, QSemaphore, QSemaphore> t3("Thread 1", lockBC, lockC);
Worker<PrintA> t1("Thread 1", lockBC);
Worker<PrintB> t2("Thread 1", lockBC, lockC);
Worker<PrintC> t3("Thread 1", lockBC, lockC);
t1.start();
t2.start();
@ -104,6 +92,6 @@ int main(int argc, char *argv[]) {
t1.wait();
t2.wait();
t3.wait();
//return app.exec();
return 0;
}