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;
}

32
P5/Semaphor.h Normal file
View File

@@ -0,0 +1,32 @@
#include <mutex>
#include <condition_variable>
#include <queue>
class Semaphor
{
private:
int count;
std::mutex mtx;
std::condition_variable cond;
std::queue<std::thread::id> wait_queue; // Beispielhafte Warteschlange
public:
Semaphor(int i) : count(i) {}
void acquire()
{
std::unique_lock<std::mutex> lock(mtx);
while (count == 0)
{ // Solange keine Ressourcen verfügbar sind, warten
cond.wait(lock);
}
count--; // Ressource belegen
}
void release()
{
std::unique_lock<std::mutex> lock(mtx);
count++; // Ressource freigeben
cond.notify_one(); // Einen wartenden Thread aufwecken
}
};

View File

@@ -1,52 +0,0 @@
#ifndef MULTITHREADING_SEMAPHORE_H
#define MULTITHREADING_SEMAPHORE_H
#include <mutex>
#include <condition_variable>
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<std::mutex> 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<std::mutex> 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

BIN
P5/build/Debug/Main.obj Normal file

Binary file not shown.

BIN
P5/build/Debug/outDebug.exe Normal file

Binary file not shown.

BIN
P5/build/Debug/outDebug.ilk Normal file

Binary file not shown.

BIN
P5/build/Debug/outDebug.pdb Normal file

Binary file not shown.

BIN
P5/build/Debug/vc140.pdb Normal file

Binary file not shown.