Skip to content

serge-klim/concurrent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

concurent::Heap

Is rather naive implementation of lock-free reference counting pointers forward iterable container. Pointer placed in the concurent::Heap stays there until it referenced by returned unique_ptr and any additional shared_ptr(s) obtained by dereferencing concurent::Heap iterators.

Motivation

One of the common approaches when using Boost.Asio or Networking TS N4656 is to create connection/session object in accept handler and pass it wrapped in std::shared_ptr to subsequent handlers. This approach allows to keep connection/session object alive until it used by any ongoing operation handler. See Boost.Asio examples: Chat, Echo or HTTP Server as example of such approach.

But sometimes it desirable to perform some additional operation on opened connection. concurent::Heap simplifies such tasks by allowing iterate its elements thread safe manner.

here is slightly modified Echo example.

#include "concurent/heap.hpp"

class server
{
public:
  server(boost::asio::io_context& io_context, short port)
    : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
  {
    do_accept();
  }

  void drop_connections()
  {
	for(auto& session : sessions_) // iterating concurent::Heap is thread safe
	   sesion->close(); // make sure this operation is thread safe 
  }

private:
  void do_accept()
  {
    acceptor_.async_accept(
        [this](boost::system::error_code ec, tcp::socket socket)
        {
          if (!ec)
          {
            //std::make_shared<session>(std::move(socket))->start();

			// session will be automaticaly removed from heap when all refferences to it are gone
			auto session = std::shared_ptr{sessions_.emplace(std::move(socket))}; 
			session->start();  
          }

          do_accept();
        });
  }

  tcp::acceptor acceptor_;
  concurent::Heap<session> sessions_;
};

Requirements

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages