Anpassungen Semaphor
This commit is contained in:
102
P5/Main.cpp
102
P5/Main.cpp
@@ -1,41 +1,48 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "Semaphore.h"
|
#include "Semaphor.h"
|
||||||
#include <process.h>
|
#include <process.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// Thread T1 - Schreibt alle Kleinbuchstaben des Alphabets
|
// Thread T1 - Schreibt alle Kleinbuchstaben des Alphabets
|
||||||
void thread1() {
|
void thread1()
|
||||||
for (char c = 'a'; c <= 'z'; ++c) {
|
{
|
||||||
|
for (char c = 'a'; c <= 'z'; ++c)
|
||||||
|
{
|
||||||
cout << c << ' ';
|
cout << c << ' ';
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thread T2 - Schreibt alle natürlichen Zahlen von 0 bis 32
|
// Thread T2 - Schreibt alle natürlichen Zahlen von 0 bis 32
|
||||||
void thread2() {
|
void thread2()
|
||||||
for (int i = 0; i <= 32; ++i) {
|
{
|
||||||
|
for (int i = 0; i <= 32; ++i)
|
||||||
|
{
|
||||||
cout << i << ' ';
|
cout << i << ' ';
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thread T3 - Schreibt alle Großbuchstaben des Alphabets
|
// Thread T3 - Schreibt alle Großbuchstaben des Alphabets
|
||||||
void thread3() {
|
void thread3()
|
||||||
for (char c = 'A'; c <= 'Z'; ++c) {
|
{
|
||||||
|
for (char c = 'A'; c <= 'Z'; ++c)
|
||||||
|
{
|
||||||
cout << c << ' ';
|
cout << c << ' ';
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void asynch_init(){
|
void asynch_init()
|
||||||
|
{
|
||||||
// Start der Threads
|
// Start der Threads
|
||||||
thread t1(thread1);
|
thread t1(thread1);
|
||||||
thread t2(thread2);
|
thread t2(thread2);
|
||||||
thread t3(thread3);
|
thread t3(thread3);
|
||||||
|
|
||||||
// Warten auf die Beendigung der Threads
|
// Warten auf die Beendigung der Threads
|
||||||
t1.join();
|
t1.join();
|
||||||
t2.join();
|
t2.join();
|
||||||
@@ -44,10 +51,12 @@ void asynch_init(){
|
|||||||
|
|
||||||
mutex mtx;
|
mutex mtx;
|
||||||
|
|
||||||
void mutexThread1() {
|
void mutexThread1()
|
||||||
|
{
|
||||||
mtx.lock();
|
mtx.lock();
|
||||||
|
|
||||||
for (char ch = 'a'; ch <= 'z'; ch++) {
|
for (char ch = 'a'; ch <= 'z'; ch++)
|
||||||
|
{
|
||||||
cout << ch << " ";
|
cout << ch << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
@@ -55,11 +64,13 @@ void mutexThread1() {
|
|||||||
mtx.unlock();
|
mtx.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mutexThread2() {
|
void mutexThread2()
|
||||||
|
{
|
||||||
|
|
||||||
mtx.lock();
|
mtx.lock();
|
||||||
|
|
||||||
for (int i = 0; i < 33; i++) {
|
for (int i = 0; i < 33; i++)
|
||||||
|
{
|
||||||
cout << i << " ";
|
cout << i << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
@@ -67,10 +78,12 @@ void mutexThread2() {
|
|||||||
mtx.unlock();
|
mtx.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mutexThread3() {
|
void mutexThread3()
|
||||||
|
{
|
||||||
mtx.lock();
|
mtx.lock();
|
||||||
|
|
||||||
for (char ch = 'A'; ch <= 'Z'; ch++) {
|
for (char ch = 'A'; ch <= 'Z'; ch++)
|
||||||
|
{
|
||||||
cout << ch << " ";
|
cout << ch << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
@@ -78,56 +91,63 @@ void mutexThread3() {
|
|||||||
mtx.unlock();
|
mtx.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mutex_init() {
|
void mutex_init()
|
||||||
|
{
|
||||||
thread t1(mutexThread1); // mutexThread1 is running
|
thread t1(mutexThread1); // mutexThread1 is running
|
||||||
thread t2(mutexThread2); // mutexThread2 is running
|
thread t2(mutexThread2); // mutexThread2 is running
|
||||||
thread t3(mutexThread3); // mutexThread3 is running
|
thread t3(mutexThread3); // mutexThread3 is running
|
||||||
t1.join(); // main thread waits for t1 to finish
|
t1.join(); // main thread waits for t1 to finish
|
||||||
t2.join(); // main thread waits for t2 to finish
|
t2.join(); // main thread waits for t2 to finish
|
||||||
t3.join(); // main thread waits for t3 to finish
|
t3.join(); // main thread waits for t3 to finish
|
||||||
}
|
}
|
||||||
|
|
||||||
Semaphore semaphore;
|
Semaphor semaphor(1);
|
||||||
|
|
||||||
void semaphoreThread1() {
|
void semaphoreThread1()
|
||||||
semaphore.wait();
|
{
|
||||||
for (char ch = 'a'; ch <= 'z'; ch++) {
|
semaphor.acquire();
|
||||||
|
for (char ch = 'a'; ch <= 'z'; ch++)
|
||||||
|
{
|
||||||
cout << ch << " ";
|
cout << ch << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
semaphore.post();
|
semaphor.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphoreThread2() {
|
void semaphoreThread2()
|
||||||
semaphore.wait();
|
{
|
||||||
for (int i = 0; i < 33; i++) {
|
semaphor.acquire();
|
||||||
|
for (int i = 0; i < 33; i++)
|
||||||
|
{
|
||||||
cout << i << " ";
|
cout << i << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
semaphore.post();
|
semaphor.release();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphoreThread3() {
|
void semaphoreThread3()
|
||||||
semaphore.wait();
|
{
|
||||||
for (char ch = 'A'; ch <= 'Z'; ch++) {
|
semaphor.acquire();
|
||||||
|
for (char ch = 'A'; ch <= 'Z'; ch++)
|
||||||
|
{
|
||||||
cout << ch << " ";
|
cout << ch << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
semaphore.post();
|
semaphor.release();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphore_init() {
|
void semaphore_init()
|
||||||
|
{
|
||||||
thread t1(semaphoreThread1); // thread1 is running
|
thread t1(semaphoreThread1); // thread1 is running
|
||||||
thread t2(semaphoreThread2); // thread2 is running
|
thread t2(semaphoreThread2); // thread2 is running
|
||||||
thread t3(semaphoreThread3); // thread3 is running
|
thread t3(semaphoreThread3); // thread3 is running
|
||||||
t1.join(); // main thread waits for t1 to finish
|
t1.join(); // main thread waits for t1 to finish
|
||||||
t2.join(); // main thread waits for t2 to finish
|
t2.join(); // main thread waits for t2 to finish
|
||||||
t3.join(); // main thread waits for t3 to finish
|
t3.join(); // main thread waits for t3 to finish
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
cout << "Asynch" << endl;
|
cout << "Asynch" << endl;
|
||||||
asynch_init();
|
asynch_init();
|
||||||
@@ -137,6 +157,6 @@ int main() {
|
|||||||
|
|
||||||
cout << "Semaphore" << endl;
|
cout << "Semaphore" << endl;
|
||||||
semaphore_init();
|
semaphore_init();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
32
P5/Semaphor.h
Normal file
32
P5/Semaphor.h
Normal 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
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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
BIN
P5/build/Debug/Main.obj
Normal file
Binary file not shown.
BIN
P5/build/Debug/outDebug.exe
Normal file
BIN
P5/build/Debug/outDebug.exe
Normal file
Binary file not shown.
BIN
P5/build/Debug/outDebug.ilk
Normal file
BIN
P5/build/Debug/outDebug.ilk
Normal file
Binary file not shown.
BIN
P5/build/Debug/outDebug.pdb
Normal file
BIN
P5/build/Debug/outDebug.pdb
Normal file
Binary file not shown.
BIN
P5/build/Debug/vc140.pdb
Normal file
BIN
P5/build/Debug/vc140.pdb
Normal file
Binary file not shown.
Reference in New Issue
Block a user