Skip to content

Instantly share code, notes, and snippets.

@rahul8590
Created June 20, 2013 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahul8590/5820784 to your computer and use it in GitHub Desktop.
Save rahul8590/5820784 to your computer and use it in GitHub Desktop.
The Scale program uses the Boost::ASIO libraries in order to execute the functions in async fashion. It also uses mutiple threads to have io.services run in each of them. I have Interfacing the C++ code with C wrapper .
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>
class Foo {
public:
static boost::asio::io_service io_service1;
static boost::asio::io_service io_service2;
static void handler1(const boost::system::error_code &ec)
{
std::cout << "5 s." << std::endl;
}
static void handler2(const boost::system::error_code &ec)
{
std::cout << "5 s." << std::endl;
}
static void run1()
{
io_service1.run();
}
static void run2()
{
io_service2.run();
}
void run()
{
boost::asio::deadline_timer timer1(Foo::io_service1, boost::posix_time::seconds(5));
timer1.async_wait(Foo::handler1);
boost::asio::deadline_timer timer2(Foo::io_service2, boost::posix_time::seconds(5));
timer2.async_wait(Foo::handler2);
boost::thread thread1(Foo::run1);
boost::thread thread2(Foo::run2);
thread1.join();
thread2.join();
}
};
boost::asio::io_service Foo::io_service1;
boost::asio::io_service Foo::io_service2;
extern "C" {
Foo* Foo_new(){ return new Foo(); }
void Foo_run(Foo* foo){ foo->run(); }
}
int main()
{
Foo *f = new Foo();
f->run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment