Simple multithreaded server in C ++?

I want to write a simple server application that will receive commands from a client application and run these commands in separate threads.

I looked at the server class. Example server in dlib

Examples of clients / servers in Boost Asio
+5
source share
3 answers

Boost Asio will make this pretty easy. See the Highscore tutorial for examples on how to use Boost for asynchronous multi-threaded I / O.

#include <boost/asio.hpp> 
#include <boost/thread.hpp> 
#include <iostream> 

void handler1(const boost::system::error_code &ec) 
{ 
  std::cout << "5 s." << std::endl; 
} 

void handler2(const boost::system::error_code &ec) 
{ 
  std::cout << "5 s." << std::endl; 
} 

boost::asio::io_service io_service; 

void run() 
{ 
  io_service.run(); 
} 

int main() 
{ 
  boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5)); 
  timer1.async_wait(handler1); 
  boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(5)); 
  timer2.async_wait(handler2); 
  boost::thread thread1(run); 
  boost::thread thread2(run); 
  thread1.join(); 
  thread2.join(); 
}
+4
source

. libevent. , libevent , . libevent , Erlang!

+3

- -, . , -.

Please note that your concept of a "multithreaded server", although not entirely incorrect, is very different from the fact that everyone else uses this phrase. This usually means one thread for each connection, rather than a response to one connection parallel to the threads.

The example you are requesting is a combination of a single-threaded synchronous server + parallel computing.

+2
source

All Articles