mirror of
https://github.com/KjellKod/g3log.git
synced 2025-01-19 08:46:42 +01:00
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
This commit is contained in:
parent
49aefe06f5
commit
e0355e766a
@ -1,60 +1,52 @@
|
|||||||
/** ==========================================================================
|
/** ==========================================================================
|
||||||
* 2010 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
* 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
|
* with no warranties. This code is yours to share, use and modify with no
|
||||||
* strings attached and no restrictions or obligations.
|
* strings attached and no restrictions or obligations.
|
||||||
* ============================================================================
|
* ============================================================================
|
||||||
*
|
*
|
||||||
* Example of a Active Object, using C++11 std::thread mechanisms to make it
|
* Example of a Active Object, using C++11 std::thread mechanisms to make it
|
||||||
* safe for thread communication.
|
* safe for thread communication.
|
||||||
*
|
*
|
||||||
* This was originally published at http://sites.google.com/site/kjellhedstrom2/active-object-with-cpp0x
|
* 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
|
* 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
|
* 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
|
* Last update 2013-09-25 by Kjell Hedstrom,
|
||||||
* http://www.justsoftwaresolutions.co.uk
|
* e-mail: hedstrom at kjellkod dot cc
|
||||||
*
|
* linkedin: http://linkedin.com/se/kjellkod */
|
||||||
* Last update 2012-10-10, by Kjell Hedstrom,
|
|
||||||
* e-mail: hedstrom at kjellkod dot cc
|
|
||||||
* linkedin: http://linkedin.com/se/kjellkod */
|
|
||||||
|
|
||||||
|
|
||||||
#include "active.h"
|
#include "active.hpp"
|
||||||
#include <cassert>
|
|
||||||
#include <iostream>
|
|
||||||
using namespace kjellkod;
|
using namespace kjellkod;
|
||||||
|
Active::Active() : done_(false) {
|
||||||
Active::Active(): done_(false){}
|
}
|
||||||
|
|
||||||
Active::~Active() {
|
Active::~Active() {
|
||||||
|
send( [this]{ done_ = true;} );
|
||||||
Callback quit_token = std::bind(&Active::doDone, this);
|
thd_.join();
|
||||||
send(quit_token); // tell thread to exit
|
|
||||||
thd_.join();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add asynchronously a work-message to queue
|
/// Add asynchronously a work-message to queue
|
||||||
void Active::send(Callback msg_){
|
void Active::send(Callback msg_) {
|
||||||
mq_.push(msg_);
|
mq_.push(msg_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Will wait for msgs if queue is empty
|
/// Will wait for msgs if queue is empty
|
||||||
// A great explanation of how this is done (using Qt's library):
|
/// A great explanation of how this is done (using Qt's library):
|
||||||
// http://doc.qt.nokia.com/stable/qwaitcondition.html
|
/// http://doc.qt.nokia.com/stable/qwaitcondition.html
|
||||||
void Active::run() {
|
void Active::run() {
|
||||||
while (!done_) {
|
while (!done_) {
|
||||||
// wait till job is available, then retrieve it and
|
// wait till job is available, then retrieve it and
|
||||||
// executes the retrieved job in this thread (background)
|
// executes the retrieved job in this thread (background)
|
||||||
Callback func;
|
Callback func;
|
||||||
mq_.wait_and_pop(func);
|
mq_.wait_and_pop(func);
|
||||||
func();
|
func();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Factory: safe construction of object before thread start
|
/// Factory: safe construction of object before thread start
|
||||||
std::unique_ptr<Active> Active::createActive(){
|
std::unique_ptr<Active> Active::createActive() {
|
||||||
std::unique_ptr<Active> aPtr(new Active());
|
std::unique_ptr<Active> aPtr(new Active());
|
||||||
aPtr->thd_ = std::thread(&Active::run, aPtr.get());
|
aPtr->thd_ = std::thread(&Active::run, aPtr.get());
|
||||||
return aPtr;
|
return aPtr;
|
||||||
}
|
}
|
||||||
|
@ -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 <thread>
|
|
||||||
#include <functional>
|
|
||||||
#include <condition_variable>
|
|
||||||
#include <mutex>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "shared_queue.h"
|
|
||||||
|
|
||||||
namespace kjellkod {
|
|
||||||
typedef std::function<void()> 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<Callback> 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<Active> createActive(); // Factory: safe construction & thread start
|
|
||||||
};
|
|
||||||
} // end namespace kjellkod
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
49
g2log/src/active.hpp
Normal file
49
g2log/src/active.hpp
Normal file
@ -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 <thread>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "shared_queue.h"
|
||||||
|
|
||||||
|
namespace kjellkod {
|
||||||
|
typedef std::function<void() > Callback;
|
||||||
|
|
||||||
|
class Active {
|
||||||
|
private:
|
||||||
|
Active(); // Construction ONLY through factory createActive();
|
||||||
|
void run();
|
||||||
|
|
||||||
|
shared_queue<Callback> mq_;
|
||||||
|
std::thread thd_;
|
||||||
|
bool done_; // finished flag : set by ~Active
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual ~Active();
|
||||||
|
void send(Callback msg_);
|
||||||
|
static std::unique_ptr<Active> createActive();
|
||||||
|
|
||||||
|
Active(const Active&) = delete;
|
||||||
|
Active& operator=(const Active&) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // kjellkod
|
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <future>
|
#include <future>
|
||||||
#include "active.h"
|
#include "active.hpp"
|
||||||
|
|
||||||
namespace g2 {
|
namespace g2 {
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "active.h"
|
#include "active.hpp"
|
||||||
#include "g2log.h"
|
#include "g2log.h"
|
||||||
#include "g2time.hpp"
|
#include "g2time.hpp"
|
||||||
#include "g2future.h"
|
#include "g2future.h"
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include "g2sinkwrapper.h"
|
#include "g2sinkwrapper.h"
|
||||||
#include "active.h"
|
#include "active.hpp"
|
||||||
#include "g2future.h"
|
#include "g2future.h"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user