griffin
griffin

Reputation: 3234

Simple multithreaded server in C++?

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

I was looking at the server class in dlib. Does anyone have experience using this? How does it compare to using Boost's Asio?

Upvotes: 5

Views: 4506

Answers (3)

Shane
Shane

Reputation: 100194

Boost Asio will do this quite easily. Have a look at the examples in the Highscore tutorial, which shows how to use Boost for asynchronous input/output with multithreading.

#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(); 
}

Upvotes: 4

Ben Voigt
Ben Voigt

Reputation: 283793

Asynchronous I/O is better in quite a few ways than the thread-per-client model. Optimal performance is actually achieve by thread-per-core, with each thread doing asynchronous I/O.

Note that your concept of "multithreaded server", while not exactly wrong, is quite different from what everyone else uses that phrase to mean. Generally it means one thread per connection and not the response to one connection parallelized across threads.

The example you're asking for is just a combination of single-threading synchronous server + parallel computation.

Upvotes: 2

user177800
user177800

Reputation:

I would try and not use threads at first. I would start with libevent. You will find that the libevent model is extremely simple and scales out way better than spawning a thread per request model. And if libevent can't handle your use case there is always Erlang!

Upvotes: 3

Related Questions