views:

123

answers:

3

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?

+3  A: 

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!

fuzzy lollipop
Boost.Asio is perfectly suitable for this kind of one-thread-to-many-clients type of work as well.
Staffan
A: 

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.

Ben Voigt
+1  A: 

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