clockTick function is not declared in this scope - c++

I am putting both the CPP and HPP files here and the error I receive:
I am adding the complete code here and the error I am encountering; kindly help.
Kindly help me in posting this question too.
CPP:
#include "TimeWindowCounter.hpp"
#include <iostream>
#include "../../Common/TimeAnalyser.hpp"
TimeWindowCounter::TimeWindowCounter()
{
init();
}
void TimeWindowCounter::init()
{
boost::atomic<bool> first_order_sent(false);
}
TimeWindowCounter::TimeWindowCounter(double currTime)
{
first_order_sent = true;
startTime = currTime;
endTime = currTime;
orderCount = 0;
ind = 0;
N = 1000;
for (int j =0; j<= 99; j++ )
numberofOrder[j] = 0;
}
TimeWindowCounter::~TimeWindowCounter()
{
}
void TimeWindowCounter::addOrder()
{
orderCount++;
numberofOrder[ind] = orderCount;
}
void TimeWindowCounter::clockTick(const boost::system::error_code& /*e*/, boost::asio::deadline_timer* t, int* count, double* currTime)
{
stopTime = *currTime;
WindowSize = timeAnalyser.getDiffTime_rt(startTime, stopTime);
if (WindowSize.count()*1000 > N)
startTime = startTime + (WindowSize.count()*1000-N);
ind = (ind + 1) % 10;
std::cout << *count << std::endl;
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::milliseconds(1));
t->async_wait(boost::bind(clockTick,boost::asio::placeholders::error, t, count, currTime));
} // 10ms or 100 times a second
int TimeWindowCounter::getNumOfOrders()
{
int sum = 0;
for (int i = 0; i<= 99; i++)
sum = sum + numberofOrder[i];
return sum;
}
void TimeWindowCounter::run(){
io.run();
}
int main()
{
boost::asio::io_service io;
int count = 0;
TimeAnalyser timeAnalyser;
double currTime = timeAnalyser.getClockTime();
TimeWindowCounter * timewindowCounter = new TimeWindowCounter();
boost::asio::deadline_timer t(io, boost::posix_time::milliseconds(10));
t.async_wait(boost::bind(clockTick,boost::asio::placeholders::error, &t, &count, &currTime));
timewindowCounter->run();
for (int j = 0; j <=100000 ; j++)
timewindowCounter->addOrder();
std::cout << "Final count is " << count << std::endl;
}
HPP:
#ifndef TIMEWINDOWCOUNTER
#define TIMEWINDOWCOUNTER
#include <iostream>
#include <boost/chrono.hpp>
#include <boost/atomic.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <cmath>
#include <unistd.h>
#include "../../Common/TimeAnalyser.hpp"
using namespace std;
class TimeWindowCounter
{
TimeAnalyser timeAnalyser;
boost::asio::io_service io;
public:
TimeWindowCounter();
~TimeWindowCounter();
TimeWindowCounter(double currTime);
boost::atomic<bool> first_order_sent;
boost::chrono::duration<double> interval;
double startTime;
double stopTime;
double endTime;
double currTime;
void init();
int numberofOrders;
int orderCount;
int numberofOrder[99];
int ind;
boost::chrono::duration<double> WindowSize;
int N; // WindowSize
void addOrder();
void clockTick(const boost::system::error_code&, boost::asio::deadline_timer* t, int* count, double* currTime); // 10ms or 100 times a second
int getNumOfOrders();
void timer_thread();
void run();
};
#endif
Compile:
g++ -std=c++11 -o TA TimeWindowCounter.cpp -lboost_chrono -lboost_system
Error:
TimeWindowCounter.cpp: In member function ‘void TimeWindowCounter::clockTick(const boost::system::error_code&, boost::asio::deadline_timer*, int*, double*)’:
TimeWindowCounter.cpp:50:93: error: invalid use of non-static member function
t->async_wait(boost::bind(clockTick,boost::asio::placeholders::error, t, count, currTime));
^
TimeWindowCounter.cpp: In function ‘int main()’:
TimeWindowCounter.cpp:80:26: error: ‘clockTick’ was not declared in this scope
t.async_wait(boost::bind(clockTick,boost::asio::placeholders::error, &t, &count, &currTime));

You're binding an instance member function.
You need to qualify the function name and pass a suitable "this" parameter as the first argument:
t->async_wait(boost::bind(&TimeWindowCounter::clockTick, this, boost::asio::placeholders::error, t, count, currTime));
and
t.async_wait(boost::bind(&TimeWindowCounter::clockTick, timewindowCounter, boost::asio::placeholders::error, &t, &count, &currTime));
respectively.
Live Demo
Live On Coliru
#ifndef TIMEWINDOWCOUNTER
#define TIMEWINDOWCOUNTER
#include <iostream>
#include <boost/chrono.hpp>
#include <boost/atomic.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <cmath>
#include <unistd.h>
//#include "../../Common/TimeAnalyser.hpp"
struct TimeAnalyser {
double getClockTime() const { return 0; }
boost::chrono::duration<double> getDiffTime_rt(double, double) const { return boost::chrono::milliseconds(1); }
};
using namespace std;
class TimeWindowCounter {
TimeAnalyser timeAnalyser;
boost::asio::io_service io;
public:
TimeWindowCounter();
~TimeWindowCounter();
TimeWindowCounter(double currTime);
boost::atomic<bool> first_order_sent;
boost::chrono::duration<double> interval;
double startTime;
double stopTime;
double endTime;
double currTime;
void init();
int numberofOrders;
int orderCount;
int numberofOrder[99];
int ind;
boost::chrono::duration<double> WindowSize;
int N; // WindowSize
void addOrder();
void clockTick(const boost::system::error_code&, boost::asio::deadline_timer* t, int* count,
double* currTime); // 10ms or 100 times a second
int getNumOfOrders();
void timer_thread();
void run();
};
#endif
//#include "TimeWindowCounter.hpp"
#include <iostream>
//#include "../../Common/TimeAnalyser.hpp"
TimeWindowCounter::TimeWindowCounter() { init(); }
void TimeWindowCounter::init() { boost::atomic<bool> first_order_sent(false); }
TimeWindowCounter::TimeWindowCounter(double currTime)
{
first_order_sent = true;
startTime = currTime;
endTime = currTime;
orderCount = 0;
ind = 0;
N = 1000;
for (int j = 0; j <= 99; j++)
numberofOrder[j] = 0;
}
TimeWindowCounter::~TimeWindowCounter() {}
void TimeWindowCounter::addOrder()
{
orderCount++;
numberofOrder[ind] = orderCount;
}
void TimeWindowCounter::clockTick(const boost::system::error_code& /*e*/, boost::asio::deadline_timer* t, int* count,
double* currTime)
{
stopTime = *currTime;
WindowSize = timeAnalyser.getDiffTime_rt(startTime, stopTime);
if (WindowSize.count() * 1000 > N)
startTime = startTime + (WindowSize.count() * 1000 - N);
ind = (ind + 1) % 10;
std::cout << *count << std::endl;
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::milliseconds(1));
t->async_wait(boost::bind(&TimeWindowCounter::clockTick, this, boost::asio::placeholders::error, t, count, currTime));
} // 10ms or 100 times a second
int TimeWindowCounter::getNumOfOrders()
{
int sum = 0;
for (int i = 0; i <= 99; i++)
sum = sum + numberofOrder[i];
return sum;
}
void TimeWindowCounter::run() { io.run(); }
int main()
{
boost::asio::io_service io;
int count = 0;
TimeAnalyser timeAnalyser;
double currTime = timeAnalyser.getClockTime();
TimeWindowCounter* timewindowCounter = new TimeWindowCounter();
boost::asio::deadline_timer t(io, boost::posix_time::milliseconds(10));
t.async_wait(boost::bind(&TimeWindowCounter::clockTick, timewindowCounter, boost::asio::placeholders::error, &t, &count, &currTime));
timewindowCounter->run();
for (int j = 0; j <= 100000; j++)
timewindowCounter->addOrder();
std::cout << "Final count is " << count << std::endl;
}
Prints
Final count is 0

Related

C++ calling a method in specified interval without sleep

I need to execute multiple methods with specified interval(1 millisecond). Is there any way to call the methods in a loop without sleep because I am not getting 1000 entries in a log file for a second
Currently I have implemented using sleep. Data collection also might take little time.
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>
#include <csignal>
#include <chrono>
#include <thread>
#include <fstream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <windows.h>
#include <atomic>
volatile std::sig_atomic_t gStatus{};
void signalHandler(int sig) {
gStatus = sig;
}
void feature1(std::string &output) { output = "feature1"; }
void feature2(std::string &output) { output = "feature2"; }
void feature3(std::string &output) { output = "feature3"; }
void feature4(std::string &output) { output = "feature4"; }
void feature5(std::string &output) { output = "feature5"; }
void feature6(std::string &output) { output = "feature6"; }
void processInfoRequest(std::string logFile, std::unordered_map<std::string, std::function<void(std::string &value)>> methodMap)
{
std::ofstream ofs(logFile);
std::uint64_t prevTime = 0, curTime = 0, diffTime = 0, tmpTime = 0, scheduleTime = 0, pollPeriod = 1;
prevTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
while (gStatus != SIGINT)
{
prevTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
prevTime = prevTime - scheduleTime;
**//get value**
auto timenow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
ofs << ctime(&timenow);
for(auto it : methodMap)
{
std::string output;
it.second(output);
ofs << output << ",";
}
ofs << "\n";
curTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
diffTime = curTime - prevTime;
if (diffTime < pollPeriod)
{
diffTime = pollPeriod - diffTime;
curTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::uint64_t i = 0;
std::uint64_t n = diffTime / 10;
std::uint64_t remainingMs = diffTime % 10;
for (i = 0; i < n; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::this_thread::sleep_for(std::chrono::milliseconds(remainingMs));
tmpTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
scheduleTime = tmpTime - (curTime + diffTime);
}
else
{
scheduleTime = 0;
}
}
ofs.close();
}
int main()
{
std::signal(SIGINT, signalHandler);
std::string output;
std::unordered_map<std::string, std::function<void(std::string &value)>> methodMap = {
{"f1", feature1},
{"f2", feature2},
{"f3", feature3},
{"f4", feature4},
{"f5", feature5},
{"f6", feature6}
};
std::thread th1 = std::thread(processInfoRequest, "threadlog.csv", methodMap);
if (th1.joinable())
th1.join();
return 0;
}

How to benchmark my system using multiple threads in c++?

I have simplified my code, and it compiles, but it doesn't do anything. It doesn't error out though either. I am trying to get 7 threads (on my 8-core processor) in this example to write to a variable to benchmark my system. I would like to do this with multiple threads to see if it's faster. It's based off other code that worked before I added multithreading. When I run, it just terminates. It should show progress each second of how many total iterations all the threads have done together. Some of the includes are there from other code I am working on.
I would like to also gracefully terminate all 7 threads when Ctrl-C is pressed. Help would be appreciated. Thanks!
//Compiled using: g++ ./test.cpp -lpthread -o ./test
#include <stdio.h>
#include <string>
#include <iostream>
#include <time.h>
#include <ctime>
#include <ratio>
#include <chrono>
#include <iomanip>
#include <locale.h>
#include <cstdlib>
#include <pthread.h>
using namespace std;
using namespace std::chrono;
const int NUM_THREADS = 7;
const std::string VALUE_TO_WRITE = "TEST";
unsigned long long int total_iterations = 0;
void * RunBenchmark(void * threadid);
class comma_numpunct: public std::numpunct < char > {
protected: virtual char do_thousands_sep() const {
return ',';
}
virtual std::string do_grouping() const {
return "\03";
}
};
void * RunBenchmark(void * threadid) {
unsigned long long int iterations = 0;
std::string benchmark;
int seconds = 0;
std::locale comma_locale(std::locale(), new comma_numpunct());
std::cout.imbue(comma_locale);
auto start = std::chrono::system_clock::now();
auto end = std::chrono::system_clock::now();
do {
start = std::chrono::system_clock::now();
while ((std::chrono::duration_cast < std::chrono::seconds > (end - start).count() != 1)) {
benchmark = VALUE_TO_WRITE;
iterations += 1;
}
total_iterations += iterations;
iterations = 0;
cout << "Total Iterations: " << std::setprecision(0) << std::fixed << total_iterations << "\r";
} while (1);
}
int main(int argc, char ** argv) {
unsigned long long int iterations = 0;
int tc, tn;
pthread_t threads[NUM_THREADS];
for (tn = 0; tn < NUM_THREADS; tn++) {
tc = pthread_create( & threads[tn], NULL, & RunBenchmark, NULL);
}
return 0;
}

C/C++ threads magic difference in condition

I wanted to write simple multithread app in C/C++. Function funProducent produces 100 values and if random generated value is in given range, char is added to buffer. Function funKonzument comsumes values from buffer. Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define BUFFER_LIMIT 20
struct struktura{
pthread_mutex_t mutex;
pthread_cond_t bufferNotFull;
pthread_cond_t bufferNotEmpty;
int bufferIndex;
char * buffer;
int junk;
};
void * funProducent(void *arg){
struktura * data = (struktura *) arg;
int i = 0;
while (i < 100) {
pthread_mutex_lock(&data->mutex);
if(data->bufferIndex == BUFFER_LIMIT - 1){
pthread_cond_wait(&data->bufferNotFull, &data->mutex);
}
int randomValue = (rand() % 20) + 1;
if( randomValue < 13 ){
data->buffer[++data->bufferIndex] = 'a';
printf("%2d : Producent at index %d added %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
pthread_cond_signal(&data->bufferNotEmpty);
} else {
data->junk++;
}
pthread_mutex_unlock(&data->mutex);
i++;
}
printf("producent is done\n");
}
void * funKonzument(void *arg){
struktura * data = (struktura *) arg;
int i = 0;
while (i + data->junk < 100) {
printf("%d\n", i + data->junk);
pthread_mutex_lock(&data->mutex);
if(data->bufferIndex < 0){
pthread_cond_wait(&data->bufferNotEmpty, &data->mutex);
}
printf("%2d : Konzument at index %d consumed %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
data->bufferIndex--;
pthread_cond_signal(&data->bufferNotFull);
pthread_mutex_unlock(&data->mutex);
i++;
}
printf("konzument is done\n");
}
int main(int argc, char** argv) {
pthread_t threadProducent, threadKonzument;
struktura threadData;
threadData.buffer = (char *) malloc(sizeof(char) * BUFFER_LIMIT);
threadData.bufferIndex = -1;
threadData.bufferNotFull = PTHREAD_COND_INITIALIZER;
threadData.bufferNotEmpty = PTHREAD_COND_INITIALIZER;
threadData.mutex = PTHREAD_MUTEX_INITIALIZER;
threadData.junk = 0;
pthread_create(&threadProducent, NULL, funProducent, &threadData);
pthread_create(&threadKonzument, NULL, funKonzument, &threadData);
pthread_join(threadProducent, NULL);
pthread_join(threadKonzument, NULL);
free(threadData.buffer);
pthread_mutex_destroy(&threadData.mutex);
pthread_cond_destroy(&threadData.bufferNotFull);
pthread_cond_destroy(&threadData.bufferNotEmpty);
return 0;
}
When I try to run this code, sometimes it stucks in funKonzument at this line:
pthread_cond_wait(&data->bufferNotEmpty, &data->mutex);
But...when I change condition in funProducent method from:
if( randomValue < 13 )
to
if( randomValue > 8 )
everything works fine. Is anyone able to explain me what magic difference is between this two conditions?
You are probably suffering from spurious wakes and some problem with the junk counter. I just removed that counter and added a cond wait loop function (and a little lock context manager) and then the hangings seems to have stopped.
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <stdexcept>
#include <functional>
#define BUFFER_LIMIT 20
struct struktura{
pthread_mutex_t mutex;
pthread_cond_t bufferNotFull;
pthread_cond_t bufferNotEmpty;
int bufferIndex;
char * buffer;
};
// a lock context manager
class mlock {
pthread_mutex_t* mtx;
public:
mlock(pthread_mutex_t& Mtx) :
mtx(&Mtx)
{
int rv=pthread_mutex_lock(mtx);
if(rv) throw std::runtime_error(std::to_string(rv));
}
mlock(const mlock&) = delete;
mlock(mlock&&) = delete;
mlock& operator=(const mlock&) = delete;
mlock& operator=(mlock&&) = delete;
~mlock() {
pthread_mutex_unlock(mtx);
}
};
// silly loop to take care of spurious wakes
void cwait(pthread_cond_t& c, pthread_mutex_t& m, std::function<bool()> f) {
while(f()) pthread_cond_wait(&c, &m);
}
void* funProducent(void *arg){
struktura* data = static_cast<struktura*>(arg);
int i = 0;
while(i < 100) {
mlock dummy(data->mutex);
cwait(data->bufferNotFull, data->mutex, [&](){return data->bufferIndex == BUFFER_LIMIT - 1;});
int randomValue = (rand() % 20) + 1;
if( randomValue < 13 ){
data->buffer[++data->bufferIndex] = 'a';
printf("%2d : Producent at index %d added %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
i++;
pthread_cond_signal(&data->bufferNotEmpty);
}
}
printf("producent is done\n");
return nullptr;
}
void* funKonzument(void *arg){
struktura* data = static_cast<struktura*>(arg);
int i = 0;
while(i < 100) {
mlock dummy(data->mutex);
cwait(data->bufferNotEmpty, data->mutex, [&](){return data->bufferIndex<0;});
printf("\t\t\t%2d : Konzument at index %d consumed %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
data->bufferIndex--;
i++;
pthread_cond_signal(&data->bufferNotFull);
}
printf("\t\t\tkonzument is done\n");
return nullptr;
}
int main() {
pthread_t threadProducent, threadKonzument;
struktura threadData;
threadData.buffer = (char *) malloc(sizeof(char) * BUFFER_LIMIT);
threadData.bufferIndex = -1;
threadData.bufferNotFull = PTHREAD_COND_INITIALIZER;
threadData.bufferNotEmpty = PTHREAD_COND_INITIALIZER;
threadData.mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&threadProducent, NULL, funProducent, &threadData);
pthread_create(&threadKonzument, NULL, funKonzument, &threadData);
pthread_join(threadProducent, NULL);
pthread_join(threadKonzument, NULL);
free(threadData.buffer);
pthread_mutex_destroy(&threadData.mutex);
pthread_cond_destroy(&threadData.bufferNotFull);
pthread_cond_destroy(&threadData.bufferNotEmpty);
return 0;
}

undefined reference to function using clock_gettime

I'm trying to create a simple test to time some functions. I'm using the following files main.cpp and testTiming.hpp. I am not able to compile, I get the following error: main.cpp:16: undefined reference to `startProfile(TIME_ID)'. Is there anything incorrect about my function definitions?
testTiming.hpp
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
enum TIME_ID
{
TIME1 = 0,
TIME2 = 1,
NUM_TIMES = 2
};
typedef struct timeTable
{
timespec lastStartTime;
timespec totalTime;
} timeTable;
timeTable mTimeTable[NUM_TIMES];
void inline stopProfile( TIME_ID timeId );
timespec inline diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec) < 0) {
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return temp;
}
void inline startprofile( TIME_ID timeId)
{
if (timeId = TIME1)
{
stopProfile(TIME1);
}
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(mTimeTable[timeId].lastStartTime));
//clock_gettime(CLOCK_REALTIME, &(mTimeTable[timeId].lastStartTime));
};
void inline stopProfile( TIME_ID timeId)
{
timespec stopTime;
timespec diffTime;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(stopTime));
//clock_gettime(CLOCK_REALTIME, &(stopTime));
diffTime = diff(mTimeTable[timeId].lastStartTime, stopTime);
mTimeTable[timeId].totalTime.tv_sec += diffTime.tv_sec;
mTimeTable[timeId].totalTime.tv_nsec += diffTime.tv_nsec;
if (timeId != TIME1)
{
startprofile(TIME1);
}
};
void printprofile(void)
{
int i;
for(i = 0; i < NUM_TIMES; i++)
{
switch( i)
{
case TIME1:
printf("PROFILE : TIME1: %f s\n", (float) mTimeTable[i].totalTime.tv_sec + float
(mTimeTable[i].totalTime.tv_nsec / 1000000000.0f) );
break;
case TIME2:
printf("PR0FILE : TIME2P: %d %f s\n", (float) mTimeTable[i].totalTime.tv_sec + float
(mTimeTable[i].totalTime.tv_nsec / 1000000000.0f) );
printf("PR0FILE : TIME2W: %d %f s\n", (float) mTimeTable[i].totalTime.tv_sec + float
(mTimeTable[i].totalTime.tv_nsec) );
break;
default:
break;
}
}
};
main.cpp
#include "testTiming.hpp"
#include <time.h>
using namespace std;
void inline startProfile( TIME_ID timeId );
void inline printProfile( void );
int main(int argc, char** argv)
{
int sec = 5;
startProfile(TIME1);
sleep(sec);
stopProfile(TIME1);
//printProfile();
return 0;
}
Makefile
ls SHELL = /bin/csh
all :
g++ main.cpp -g -o timeTest
clean:
rm -f timeTest

Boost Threads and Timers, C++

I have this code for a custom class 'sau_timer':
sau_timer::sau_timer(int secs, timerparam f, vector<string> params) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
this->params = params;
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
boost::thread thrd(boost::bind(&boost::asio::io_service::run, &io));
io.run();
}
void sau_timer::exec(const boost::system::error_code&) {
(f)(params);
}
I want it so that when I make a sau_timer object, the timer will start, but allow program execution to continue. For example, this is main():
int main(int argc, char* argv[])
{
vector<string> args(1);
args[0] = "Hello!";
sau_timer timer_test(3, sau_prompt, args);
args[0] = "First!";
sau_prompt(args);
timer_test.thrd.join();
return 0;
}
My intention here is that timer_test is made, starting a timer that waits three seconds before calling sau_prompt("Hello!"), but that sau_prompt("First!") will be called first. At the moment, Hello is shown in the prompt before First, indicating that the timer is halting the entire program for three seconds before allowing it to continue. I want the timer to run in the background.
What am I doing wrong? The code compiles...
Thank you.
You're calling "io.run()" in sau_timer - that essentially tells the asio reactor to process any/all pending async events if it can.
You should call run or post after the having setup the events, which is how its normally done. check out the examples in the asio documentation.
#include <iostream>
#include <asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/date_time.hpp>
#include <boost/thread.hpp>
class event_timer
{
public:
event_timer(asio::io_service& io_service,
const std::size_t& tid,
const std::size_t& interval = 5)
: io_service_(io_service),
timer_(io_service),
tid_(tid),
interval_(interval),
tick_count_(0),
total_diff_(0)
{
}
void start()
{
timer_.cancel();
initiate_timer();
}
void stop()
{
timer_.cancel();
}
void set_interval(const std::size_t& milliseconds)
{
interval_ = milliseconds;
}
private:
inline void initiate_timer()
{
if (interval_)
{
timer_.expires_from_now(boost::posix_time::milliseconds(interval_));
timer_.async_wait(
boost::bind(&event_timer::handle_timer_event,this,
asio::placeholders::error));
before_ = boost::posix_time::microsec_clock::universal_time();
}
}
inline void handle_timer_event(const asio::error_code& error)
{
if (!error && interval_)
{
after_ = boost::posix_time::microsec_clock::universal_time();
boost::posix_time::time_duration duration = after_ - before_;
total_diff_ += std::abs(interval_ - duration.total_milliseconds());
++tick_count_;
if (tick_count_ < 200)
initiate_timer();
else
std::cout << "Timer["<< tid_ <<"]\tTick["<< tick_count_ <<"] Average Diff: " << total_diff_ / (1.0 * tick_count_) << std::endl;
}
}
asio::io_service& io_service_;
std::size_t tid_;
std::size_t interval_;
std::size_t tick_count_;
asio::deadline_timer timer_;
boost::posix_time::ptime before_;
boost::posix_time::ptime after_;
std::size_t total_diff_;
};
int main()
{
std::cout << "Timer Test" << std::endl;
asio::io_service io_service;
try
{
const std::size_t et_cnt = 1000;
std::vector<event_timer*> et_lst;
for(unsigned int i = 0; i < et_cnt; ++i)
{
et_lst.push_back(new event_timer(io_service,i,10));
}
for(unsigned int i = 0; i < et_cnt;)
{
et_lst[i++]->start();
}
std::size_t thread_pool_size = 100;
//Create a pool of threads to run all of the io_services.
std::vector<boost::shared_ptr<boost::thread> > threads;
for (std::size_t i = 0; i < thread_pool_size; ++i)
{
boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&asio::io_service::run, &io_service)));
threads.push_back(thread);
}
// Wait for all threads in the pool to exit.
for (std::size_t i = 0; i < threads.size(); ++i)
threads[i]->join();
for(unsigned int i = 0; i < et_cnt; delete et_lst[i++]);
}
catch(std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}
considering namespace and boost version 1.69, there are three modifications should be done:
change #include <asio.hpp> into #include <boost/asio.hpp>
add: using namespace boost; using namespace boost:asio;
change inline void handle_timer_event(const asio::error_code& error) into void handle_timer_event(const boost::system::error_code& error)