From e0355e766afb902e861d8dcf1f440e36dcd0d0b0 Mon Sep 17 00:00:00 2001 From: KjellKod Date: Wed, 25 Sep 2013 21:39:11 -0600 Subject: [PATCH] active using lambda instead of std::function for exit. Cleanup of includes. Renamed to hpp --HG-- rename : g2log/src/active.h => g2log/src/active.hpp --- g2log/src/active.cpp | 84 ++++++++++++++++++--------------------- g2log/src/active.h | 57 -------------------------- g2log/src/active.hpp | 49 +++++++++++++++++++++++ g2log/src/g2future.h | 2 +- g2log/src/g2logworker.cpp | 2 +- g2log/src/g2sink.h | 2 +- 6 files changed, 90 insertions(+), 106 deletions(-) delete mode 100644 g2log/src/active.h create mode 100644 g2log/src/active.hpp diff --git a/g2log/src/active.cpp b/g2log/src/active.cpp index 4160a37..cf67496 100644 --- a/g2log/src/active.cpp +++ b/g2log/src/active.cpp @@ -1,60 +1,52 @@ /** ========================================================================== -* 2010 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes -* with no warranties. This code is yours to share, use and modify with no -* strings attached and no restrictions or obligations. -* ============================================================================ -* -* Example of a Active Object, using C++11 std::thread mechanisms to make it -* safe for thread communication. -* -* This was originally published at http://sites.google.com/site/kjellhedstrom2/active-object-with-cpp0x -* and inspired from Herb Sutter's C++11 Active Object -* http://herbsutter.com/2010/07/12/effective-concurrency-prefer-using-active-objects-instead-of-naked-threads -* -* The code below uses JustSoftware Solutions Inc std::thread implementation -* http://www.justsoftwaresolutions.co.uk -* -* Last update 2012-10-10, by Kjell Hedstrom, -* e-mail: hedstrom at kjellkod dot cc -* linkedin: http://linkedin.com/se/kjellkod */ + * 2010 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes + * with no warranties. This code is yours to share, use and modify with no + * strings attached and no restrictions or obligations. + * ============================================================================ + * + * Example of a Active Object, using C++11 std::thread mechanisms to make it + * safe for thread communication. + * + * This was originally published at http://sites.google.com/site/kjellhedstrom2/active-object-with-cpp0x + * and inspired from Herb Sutter's C++11 Active Object + * http://herbsutter.com/2010/07/12/effective-concurrency-prefer-using-active-objects-instead-of-naked-threads + * + * Last update 2013-09-25 by Kjell Hedstrom, + * e-mail: hedstrom at kjellkod dot cc + * linkedin: http://linkedin.com/se/kjellkod */ -#include "active.h" -#include -#include +#include "active.hpp" using namespace kjellkod; - -Active::Active(): done_(false){} - +Active::Active() : done_(false) { +} Active::~Active() { - - Callback quit_token = std::bind(&Active::doDone, this); - send(quit_token); // tell thread to exit - thd_.join(); + send( [this]{ done_ = true;} ); + thd_.join(); } -// Add asynchronously a work-message to queue -void Active::send(Callback msg_){ - mq_.push(msg_); +/// Add asynchronously a work-message to queue +void Active::send(Callback msg_) { + mq_.push(msg_); } -// Will wait for msgs if queue is empty -// A great explanation of how this is done (using Qt's library): -// http://doc.qt.nokia.com/stable/qwaitcondition.html +/// Will wait for msgs if queue is empty +/// A great explanation of how this is done (using Qt's library): +/// http://doc.qt.nokia.com/stable/qwaitcondition.html void Active::run() { - while (!done_) { - // wait till job is available, then retrieve it and - // executes the retrieved job in this thread (background) - Callback func; - mq_.wait_and_pop(func); - func(); - } + while (!done_) { + // wait till job is available, then retrieve it and + // executes the retrieved job in this thread (background) + Callback func; + mq_.wait_and_pop(func); + func(); + } } -// Factory: safe construction of object before thread start -std::unique_ptr Active::createActive(){ - std::unique_ptr aPtr(new Active()); - aPtr->thd_ = std::thread(&Active::run, aPtr.get()); - return aPtr; +/// Factory: safe construction of object before thread start +std::unique_ptr Active::createActive() { + std::unique_ptr aPtr(new Active()); + aPtr->thd_ = std::thread(&Active::run, aPtr.get()); + return aPtr; } diff --git a/g2log/src/active.h b/g2log/src/active.h deleted file mode 100644 index 9d51600..0000000 --- a/g2log/src/active.h +++ /dev/null @@ -1,57 +0,0 @@ -/** ========================================================================== -* 2010 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes -* with no warranties. This code is yours to share, use and modify with no -* strings attached and no restrictions or obligations. -* ============================================================================ -* -* Example of a Active Object, using C++11 std::thread mechanisms to make it -* safe for thread communication. -* -* This was originally published at http://sites.google.com/site/kjellhedstrom2/active-object-with-cpp0x -* and inspired from Herb Sutter's C++11 Active Object -* http://herbsutter.com/2010/07/12/effective-concurrency-prefer-using-active-objects-instead-of-naked-threads -* -* The code below uses JustSoftware Solutions Inc std::thread implementation -* http://www.justsoftwaresolutions.co.uk -* -* Last update 2012-10-10, by Kjell Hedstrom, -* e-mail: hedstrom at kjellkod dot cc -* linkedin: http://linkedin.com/se/kjellkod */ - -#ifndef ACTIVE_H_ -#define ACTIVE_H_ - -#include -#include -#include -#include -#include - -#include "shared_queue.h" - -namespace kjellkod { -typedef std::function Callback; - -class Active { -private: - Active(const Active&); // c++11 feature not yet in vs2010 = delete; - Active& operator=(const Active&); // c++11 feature not yet in vs2010 = delete; - Active(); // Construction ONLY through factory createActive(); - void doDone(){done_ = true;} - void run(); - - shared_queue mq_; - std::thread thd_; - bool done_; // finished flag to be set through msg queue by ~Active - - -public: - virtual ~Active(); - void send(Callback msg_); - static std::unique_ptr createActive(); // Factory: safe construction & thread start -}; -} // end namespace kjellkod - - - -#endif diff --git a/g2log/src/active.hpp b/g2log/src/active.hpp new file mode 100644 index 0000000..af0e528 --- /dev/null +++ b/g2log/src/active.hpp @@ -0,0 +1,49 @@ +/** ========================================================================== + * 2010 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes + * with no warranties. This code is yours to share, use and modify with no + * strings attached and no restrictions or obligations. + * ============================================================================ + * + * Example of a Active Object, using C++11 std::thread mechanisms to make it + * safe for thread communication. + * + * This was originally published at http://sites.google.com/site/kjellhedstrom2/active-object-with-cpp0x + * and inspired from Herb Sutter's C++11 Active Object + * http://herbsutter.com/2010/07/12/effective-concurrency-prefer-using-active-objects-instead-of-naked-threads + * + * Last update 2013-09-25 by Kjell Hedstrom, + * e-mail: hedstrom at kjellkod dot cc + * linkedin: http://linkedin.com/se/kjellkod */ + +#pragma once + +#include +#include +#include + +#include "shared_queue.h" + +namespace kjellkod { + typedef std::function Callback; + + class Active { + private: + Active(); // Construction ONLY through factory createActive(); + void run(); + + shared_queue mq_; + std::thread thd_; + bool done_; // finished flag : set by ~Active + + + public: + virtual ~Active(); + void send(Callback msg_); + static std::unique_ptr createActive(); + + Active(const Active&) = delete; + Active& operator=(const Active&) = delete; + }; + + +} // kjellkod diff --git a/g2log/src/g2future.h b/g2log/src/g2future.h index 0ce1a77..0fffdfb 100644 --- a/g2log/src/g2future.h +++ b/g2log/src/g2future.h @@ -25,7 +25,7 @@ #include -#include "active.h" +#include "active.hpp" namespace g2 { diff --git a/g2log/src/g2logworker.cpp b/g2log/src/g2logworker.cpp index 63249af..256d765 100644 --- a/g2log/src/g2logworker.cpp +++ b/g2log/src/g2logworker.cpp @@ -16,7 +16,7 @@ -#include "active.h" +#include "active.hpp" #include "g2log.h" #include "g2time.hpp" #include "g2future.h" diff --git a/g2log/src/g2sink.h b/g2log/src/g2sink.h index 3d0f201..338a518 100644 --- a/g2log/src/g2sink.h +++ b/g2log/src/g2sink.h @@ -12,7 +12,7 @@ #include #include "g2sinkwrapper.h" -#include "active.h" +#include "active.hpp" #include "g2future.h"