Anpassungen Semaphor

This commit is contained in:
S170H
2024-01-21 00:03:33 +01:00
parent 6655d01102
commit fcdc4d17c1
8 changed files with 93 additions and 93 deletions

View File

@@ -1,41 +1,48 @@
#include <iostream>
#include <thread>
#include <mutex>
#include "Semaphore.h"
#include "Semaphor.h"
#include <process.h>
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;
}