Boost Asio IO Custom Object

I am new to ASIO boosting. I was reading this comparison: how does libuv compare to Boost / ASIO? (Dead link)

It turned out that it is possible to use the asio event loop for reasons other than socket programming. I am planning on using this asynchronous event loop to handle messages coming from the UI.
So far I have a parallel queue and I would like to implement my own IO boost :: asio object. The goal is to use this object, as you can see here:

#include <iostream>
#include <boost/asio.hpp>
#include "MProcessor.h"
#include "concurrent_queue.h"

void foo(const boost::system::error_code& /*e*/)
{
  std::cout << "got message" << std::endl;
}

int main()
{
  concurrent_queue<int> messages;
  /* ... */
  boost::asio::io_service io;

  MProcessor t(io, &messages);
  t.async_wait(&foo);

  io.run();

  return 0;
}

      

So the goal is to keep the io_service busy with an IO object that can call the function asynchronously (foo) when the queue (messages) has a message that can be retrieved. The queue has a method named wait_and_pop . It blocks the caller's thread until something pops up in the queue and then returns with the value of the selected item.
I got stuck at this stage and I couldn't find any online BooSIO tutorials without accompanying documents. There are a few similar questions, but they all use sockets. Any idea how to implement your own I / O object? Or am I missing some important obstacles? Many thanks!

+3


source to share





All Articles