diff --git a/P5/Main.cpp b/P5/Main.cpp index 5e8fec7..5763b41 100644 --- a/P5/Main.cpp +++ b/P5/Main.cpp @@ -1,41 +1,48 @@ #include #include #include -#include "Semaphore.h" +#include "Semaphor.h" #include using namespace std; // Thread T1 - Schreibt alle Kleinbuchstaben des Alphabets -void thread1() { - for (char c = 'a'; c <= 'z'; ++c) { +void thread1() +{ + for (char c = 'a'; c <= 'z'; ++c) + { cout << c << ' '; } cout << endl; } // Thread T2 - Schreibt alle natürlichen Zahlen von 0 bis 32 -void thread2() { - for (int i = 0; i <= 32; ++i) { +void thread2() +{ + for (int i = 0; i <= 32; ++i) + { cout << i << ' '; } cout << endl; } // Thread T3 - Schreibt alle Großbuchstaben des Alphabets -void thread3() { - for (char c = 'A'; c <= 'Z'; ++c) { +void thread3() +{ + for (char c = 'A'; c <= 'Z'; ++c) + { cout << c << ' '; } cout << endl; } -void asynch_init(){ +void asynch_init() +{ // Start der Threads thread t1(thread1); thread t2(thread2); thread t3(thread3); - + // Warten auf die Beendigung der Threads t1.join(); t2.join(); @@ -44,10 +51,12 @@ void asynch_init(){ mutex mtx; -void mutexThread1() { +void mutexThread1() +{ mtx.lock(); - for (char ch = 'a'; ch <= 'z'; ch++) { + for (char ch = 'a'; ch <= 'z'; ch++) + { cout << ch << " "; } cout << endl; @@ -55,11 +64,13 @@ void mutexThread1() { mtx.unlock(); } -void mutexThread2() { - +void mutexThread2() +{ + mtx.lock(); - for (int i = 0; i < 33; i++) { + for (int i = 0; i < 33; i++) + { cout << i << " "; } cout << endl; @@ -67,10 +78,12 @@ void mutexThread2() { mtx.unlock(); } -void mutexThread3() { +void mutexThread3() +{ mtx.lock(); - for (char ch = 'A'; ch <= 'Z'; ch++) { + for (char ch = 'A'; ch <= 'Z'; ch++) + { cout << ch << " "; } cout << endl; @@ -78,56 +91,63 @@ void mutexThread3() { mtx.unlock(); } -void mutex_init() { +void mutex_init() +{ thread t1(mutexThread1); // mutexThread1 is running thread t2(mutexThread2); // mutexThread2 is running thread t3(mutexThread3); // mutexThread3 is running - t1.join(); // main thread waits for t1 to finish - t2.join(); // main thread waits for t2 to finish - t3.join(); // main thread waits for t3 to finish + t1.join(); // main thread waits for t1 to finish + t2.join(); // main thread waits for t2 to finish + t3.join(); // main thread waits for t3 to finish } -Semaphore semaphore; +Semaphor semaphor(1); -void semaphoreThread1() { - semaphore.wait(); - for (char ch = 'a'; ch <= 'z'; ch++) { +void semaphoreThread1() +{ + semaphor.acquire(); + for (char ch = 'a'; ch <= 'z'; ch++) + { cout << ch << " "; } cout << endl; - semaphore.post(); + semaphor.release(); } -void semaphoreThread2() { - semaphore.wait(); - for (int i = 0; i < 33; i++) { +void semaphoreThread2() +{ + semaphor.acquire(); + for (int i = 0; i < 33; i++) + { cout << i << " "; } cout << endl; - semaphore.post(); - + semaphor.release(); } -void semaphoreThread3() { - semaphore.wait(); - for (char ch = 'A'; ch <= 'Z'; ch++) { +void semaphoreThread3() +{ + semaphor.acquire(); + for (char ch = 'A'; ch <= 'Z'; ch++) + { cout << ch << " "; } cout << endl; - semaphore.post(); - + semaphor.release(); } -void semaphore_init() { +void semaphore_init() +{ thread t1(semaphoreThread1); // thread1 is running thread t2(semaphoreThread2); // thread2 is running thread t3(semaphoreThread3); // thread3 is running - t1.join(); // main thread waits for t1 to finish - t2.join(); // main thread waits for t2 to finish - t3.join(); // main thread waits for t3 to finish + t1.join(); // main thread waits for t1 to finish + t2.join(); // main thread waits for t2 to finish + t3.join(); // main thread waits for t3 to finish } -int main() { +int main() +{ cout << "Asynch" << endl; asynch_init(); @@ -137,6 +157,6 @@ int main() { cout << "Semaphore" << endl; semaphore_init(); - + return 0; } \ No newline at end of file diff --git a/P5/Semaphor.h b/P5/Semaphor.h new file mode 100644 index 0000000..d0de503 --- /dev/null +++ b/P5/Semaphor.h @@ -0,0 +1,32 @@ +#include +#include +#include + +class Semaphor +{ +private: + int count; + std::mutex mtx; + std::condition_variable cond; + std::queue wait_queue; // Beispielhafte Warteschlange + +public: + Semaphor(int i) : count(i) {} + + void acquire() + { + std::unique_lock lock(mtx); + while (count == 0) + { // Solange keine Ressourcen verfügbar sind, warten + cond.wait(lock); + } + count--; // Ressource belegen + } + + void release() + { + std::unique_lock lock(mtx); + count++; // Ressource freigeben + cond.notify_one(); // Einen wartenden Thread aufwecken + } +}; \ No newline at end of file diff --git a/P5/Semaphore.h b/P5/Semaphore.h deleted file mode 100644 index d55117b..0000000 --- a/P5/Semaphore.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef MULTITHREADING_SEMAPHORE_H -#define MULTITHREADING_SEMAPHORE_H -#include -#include - -class Semaphore { -private: - std::mutex mtx; - std::condition_variable cv; - int signal; - -public: - Semaphore(int signal_ = 1) : signal(signal_) { } - - /** - * atomic function - */ - - inline void post() { - std::unique_lock lock(mtx); - signal++; - - //notify the waiting thread - //unblock(), waiting queue it not empty, wake up call thread from waiting queue - - cv.notify_one(); - - /** Notify one - * Unblocks one of the threads currently waiting for this condition. - * If no threads are waiting, the function does nothing. - */ - } - - /** - * atomic function - */ - - inline void wait() { - std::unique_lock lock(mtx); - if (signal <= 0) { - - // wait on the mutex until notify is called - // block(), put this thread into waiting status, sleep and put in waiting queue - cv.wait(lock); - } - if (signal > 0) { - signal--; - } - } - -}; -#endif //MULTITHREADING_SEMAPHORE_H diff --git a/P5/build/Debug/Main.obj b/P5/build/Debug/Main.obj new file mode 100644 index 0000000..dba52ac Binary files /dev/null and b/P5/build/Debug/Main.obj differ diff --git a/P5/build/Debug/outDebug.exe b/P5/build/Debug/outDebug.exe new file mode 100644 index 0000000..ed69a49 Binary files /dev/null and b/P5/build/Debug/outDebug.exe differ diff --git a/P5/build/Debug/outDebug.ilk b/P5/build/Debug/outDebug.ilk new file mode 100644 index 0000000..0e9d8b1 Binary files /dev/null and b/P5/build/Debug/outDebug.ilk differ diff --git a/P5/build/Debug/outDebug.pdb b/P5/build/Debug/outDebug.pdb new file mode 100644 index 0000000..8d966f5 Binary files /dev/null and b/P5/build/Debug/outDebug.pdb differ diff --git a/P5/build/Debug/vc140.pdb b/P5/build/Debug/vc140.pdb new file mode 100644 index 0000000..1ff010a Binary files /dev/null and b/P5/build/Debug/vc140.pdb differ