From 86d129e3354bf80f9758029ebfe4e606570be64b Mon Sep 17 00:00:00 2001 From: selim Date: Thu, 28 Aug 2014 22:20:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A3=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B3=D0=BE=D0=BD=D0=BA=D0=B0=20=D0=B7=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=BC=D0=B8=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=B8?= =?UTF-8?q?=20=D0=BB=D0=BE=D0=B3=D0=B3=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- include/log_queue.h | 4 +++- include/logger.h | 10 +++++----- src/log_queue.cpp | 11 ++++++++++- src/logger.cpp | 5 +++++ test/main.cpp | 32 ++++++++++++++++++++++++++------ 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 647a95f..2239130 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,8 @@ OS = linux # C++ compier CXX = g++ -CXXFLAGS = -MMD -c -g -Og -Wall -Werror -std=c++11 -Iinclude -Isrc/asm/include -LDFLAGS = -lgtest +CXXFLAGS = -MMD -c -g -Og -Wall -Werror -std=c++11 -Iinclude -Isrc/asm/include #-fsanitize=thread -fPIE +LDFLAGS = -lgtest #-ltsan -pie # Archiver AR = ar diff --git a/include/log_queue.h b/include/log_queue.h index 3747559..3c22a17 100644 --- a/include/log_queue.h +++ b/include/log_queue.h @@ -6,14 +6,16 @@ #include #include #include +#include class log_queue { private: - std::thread _worker; std::mutex _mutex; std::condition_variable _cond; std::queue> _queue; + std::atomic _working; + std::thread _worker; private: void worker_func(); diff --git a/include/logger.h b/include/logger.h index c93c4af..fe46a6d 100644 --- a/include/logger.h +++ b/include/logger.h @@ -19,16 +19,16 @@ private: public: logger(); - void set_enabled(bool enabled) - { - _enabled = enabled; - } + void set_enabled(bool enabled); template void log(const char* s, const Args&... args) { + std::cout << "step 1" << std::endl; if(_enabled) { - _queue.add_log([this, s, &args...] { + std::cout << "step 2" << std::endl; + _queue.add_log([this, s, args...] { + std::cout << "step 3" << std::endl; _logLine.str(""); log_internal(s, args...); }); diff --git a/src/log_queue.cpp b/src/log_queue.cpp index 2ab9734..cae408c 100644 --- a/src/log_queue.cpp +++ b/src/log_queue.cpp @@ -1,12 +1,17 @@ #include "log_queue.h" #include -log_queue::log_queue(): _worker(&log_queue::worker_func, this) +log_queue::log_queue(): _working(true), _worker(&log_queue::worker_func, this) { } log_queue::~log_queue() { + std::unique_lock lock(_mutex); + _queue.push([]{}); + _working = false; + _cond.notify_one(); + lock.unlock(); _worker.join(); } @@ -18,6 +23,10 @@ void log_queue::worker_func() _cond.wait(lock, [this] { return !_queue.empty(); }); std::function func = _queue.front(); _queue.pop(); + + if(_queue.empty() && !_working) + return; + lock.unlock(); func(); diff --git a/src/logger.cpp b/src/logger.cpp index 5e9adf2..22085e9 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -8,6 +8,11 @@ logger::logger(): _enabled(true) { } +void logger::set_enabled(bool enabled) +{ + _enabled = enabled; +} + void logger::log_internal(const char *s) { while (*s) diff --git a/test/main.cpp b/test/main.cpp index 892e3a8..c3d21f8 100755 --- a/test/main.cpp +++ b/test/main.cpp @@ -561,18 +561,38 @@ TEST(Ustring, replace_string) EXPECT_TRUE(str2 == "33 hello 33 world 33 more 33 and 33 more 33"); } +void func(std::size_t n) +{ + for(std::size_t j = 0; j < 100; ++j) + { + ulog.log("thread %, line %", n, j); + } + + std::cout << "end of thread " << n << std::endl; +} + int main(int argc, char **argv) { //::testing::InitGoogleTest(&argc, argv); //return RUN_ALL_TESTS(); - int one = 1; - double two = 2.5; - ulog.log("one: %, two: %", one, two); - ulog.log("Hello world!"); + const std::size_t threadsCount = 2; + //int one = 1; + //double two = 2.5; + //ulog.log("one: %, two: %", one, two); + //ulog.log("Hello world!"); + //ulog.log("some string: %", "qwe!"); - int i = 0; - std::cin >> i; + std::thread threads[threadsCount]; + for(std::size_t i = 0; i < threadsCount; ++i) + { + threads[i] = std::thread(func, i); + } + + std::cout << "the end" << std::endl; + + for(std::size_t i = 0; i < threadsCount; ++i) + threads[i].join(); return 0; }