Skip to content

Instantly share code, notes, and snippets.

@amedama41
Created December 9, 2017 01:16
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 amedama41/2671a96683eddf0684aa1f8733e5174a to your computer and use it in GitHub Desktop.
Save amedama41/2671a96683eddf0684aa1f8733e5174a to your computer and use it in GitHub Desktop.
#include <cstdint>
#include <iostream>
#include <string>
#include <boost/timer/timer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/strand.hpp>
struct handler
{
void operator()()
{
++counter;
}
static std::uint32_t counter;
};
std::uint32_t handler::counter = 0;
int main(int argc, char const* argv[])
{
if (argc < 3) {
std::cout << argv[0] << " #task (wrap|dispatch)" << std::endl;
return 0;
}
auto const max_task = std::stoull(argv[1]);
namespace asio = boost::asio;
asio::io_service io_service{};
asio::io_service::strand strand{io_service};
auto const command = std::string{argv[2]};
if (command == "wrap") {
for (auto i = 0ULL; i < max_task; ++i) {
io_service.post(strand.wrap(handler{}));
}
}
else if (command == "dispatch") {
for (auto i = 0ULL; i < max_task; ++i) {
io_service.post([&]{ strand.dispatch(handler{}); });
}
}
else if (command == "post") {
for (auto i = 0ULL; i < max_task; ++i) {
io_service.post([&]{ strand.post(handler{}); });
}
}
else if (command == "strand") {
for (auto i = 0ULL; i < max_task; ++i) {
strand.post(handler{});
}
}
{
boost::timer::auto_cpu_timer timer{};
io_service.run();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment