From c2a5995e7e656f971c0e17aa9ab988ae903916a5 Mon Sep 17 00:00:00 2001 From: marcelb Date: Thu, 27 Jul 2023 18:32:14 +0200 Subject: [PATCH] Add semaphore for work wit multithread --- .vscode/settings.json | 3 ++- example/2023-07-27.log | 3 +++ lib/log.hpp | 2 ++ src/log.cpp | 2 ++ 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 example/2023-07-27.log diff --git a/.vscode/settings.json b/.vscode/settings.json index 1439fff..75ddc7f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "iosfwd": "cpp", "fstream": "cpp", "*.tcc": "cpp", - "ostream": "cpp" + "ostream": "cpp", + "mutex": "cpp" } } \ No newline at end of file diff --git a/example/2023-07-27.log b/example/2023-07-27.log new file mode 100644 index 0000000..c31f511 --- /dev/null +++ b/example/2023-07-27.log @@ -0,0 +1,3 @@ +18:29:30 [EVENT] Start loging +18:29:44 [EVENT] Start loging +18:29:47 [EVENT] Start loging diff --git a/lib/log.hpp b/lib/log.hpp index 176e936..976494a 100644 --- a/lib/log.hpp +++ b/lib/log.hpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace std; @@ -19,6 +20,7 @@ class log { struct tm * moment; uint day; string path; + mutex io; log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false); diff --git a/src/log.cpp b/src/log.cpp index 411e58b..fe37174 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -43,6 +43,7 @@ void log::setMoment() { } void log::put(string logline) { + io.lock(); if (printInConsole) { cout << logline << endl; } @@ -74,6 +75,7 @@ void log::put(string logline) { if (!isKeepOpen && logfile.is_open()) { loose(); } + io.unlock(); }