P5 initial
This commit is contained in:
18
P5/.vscode/c_cpp_properties.json
vendored
Normal file
18
P5/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "windows-msvc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "cl.exe",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++14",
|
||||
"intelliSenseMode": "windows-msvc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
15
P5/.vscode/launch.json
vendored
Normal file
15
P5/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppvsdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"console": "externalTerminal",
|
||||
"cwd": "c:/Users/Safak/SynologyDrive/UNI/3. Semester/53107 ARBK/Praktikumsunterlagen/ARBKVS-Praktika/P5",
|
||||
"program": "c:/Users/Safak/SynologyDrive/UNI/3. Semester/53107 ARBK/Praktikumsunterlagen/ARBKVS-Praktika/P5/build/Debug/outDebug"
|
||||
}
|
||||
]
|
||||
}
|
||||
115
P5/.vscode/settings.json
vendored
Normal file
115
P5/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "c++14",
|
||||
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvarsall.bat",
|
||||
"C_Cpp_Runner.useMsvc": true,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false,
|
||||
"files.associations": {
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"cctype": "cpp",
|
||||
"charconv": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"exception": "cpp",
|
||||
"format": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"ios": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"iterator": "cpp",
|
||||
"limits": "cpp",
|
||||
"locale": "cpp",
|
||||
"memory": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ratio": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"system_error": "cpp",
|
||||
"thread": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"utility": "cpp",
|
||||
"xfacet": "cpp",
|
||||
"xiosbase": "cpp",
|
||||
"xlocale": "cpp",
|
||||
"xlocbuf": "cpp",
|
||||
"xlocinfo": "cpp",
|
||||
"xlocmes": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xlocnum": "cpp",
|
||||
"xloctime": "cpp",
|
||||
"xmemory": "cpp",
|
||||
"xstring": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp"
|
||||
}
|
||||
}
|
||||
142
P5/Main.cpp
Normal file
142
P5/Main.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include "Semaphore.h"
|
||||
#include <process.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Thread T1 - Schreibt alle Kleinbuchstaben des Alphabets
|
||||
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) {
|
||||
cout << i << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// Thread T3 - Schreibt alle Großbuchstaben des Alphabets
|
||||
void thread3() {
|
||||
for (char c = 'A'; c <= 'Z'; ++c) {
|
||||
cout << c << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
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();
|
||||
t3.join();
|
||||
}
|
||||
|
||||
mutex mtx;
|
||||
|
||||
void mutexThread1() {
|
||||
mtx.lock();
|
||||
|
||||
for (char ch = 'a'; ch <= 'z'; ch++) {
|
||||
cout << ch << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
mtx.unlock();
|
||||
}
|
||||
|
||||
void mutexThread2() {
|
||||
|
||||
mtx.lock();
|
||||
|
||||
for (int i = 0; i < 33; i++) {
|
||||
cout << i << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
mtx.unlock();
|
||||
}
|
||||
|
||||
void mutexThread3() {
|
||||
mtx.lock();
|
||||
|
||||
for (char ch = 'A'; ch <= 'Z'; ch++) {
|
||||
cout << ch << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
mtx.unlock();
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Semaphore semaphore;
|
||||
|
||||
void semaphoreThread1() {
|
||||
semaphore.wait();
|
||||
for (char ch = 'a'; ch <= 'z'; ch++) {
|
||||
cout << ch << " ";
|
||||
}
|
||||
cout << endl;
|
||||
semaphore.post();
|
||||
}
|
||||
|
||||
void semaphoreThread2() {
|
||||
semaphore.wait();
|
||||
for (int i = 0; i < 33; i++) {
|
||||
cout << i << " ";
|
||||
}
|
||||
cout << endl;
|
||||
semaphore.post();
|
||||
|
||||
}
|
||||
|
||||
void semaphoreThread3() {
|
||||
semaphore.wait();
|
||||
for (char ch = 'A'; ch <= 'Z'; ch++) {
|
||||
cout << ch << " ";
|
||||
}
|
||||
cout << endl;
|
||||
semaphore.post();
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
cout << "Asynch" << endl;
|
||||
asynch_init();
|
||||
|
||||
cout << "Mutex" << endl;
|
||||
mutex_init();
|
||||
|
||||
cout << "Semaphore" << endl;
|
||||
semaphore_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
52
P5/Semaphore.h
Normal file
52
P5/Semaphore.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user