invocation_benchmark.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* multi-threaded signal invocation benchmark */
  2. // Copyright Frank Mori Hess 2007-2008.
  3. // Distributed under the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <boost/bind.hpp>
  9. #include <boost/signals2.hpp>
  10. #include <boost/thread/thread.hpp>
  11. typedef boost::signals2::signal<void ()> signal_type;
  12. void myslot()
  13. {
  14. /* std::cout << __FUNCTION__ << std::endl;
  15. sleep(1);*/
  16. }
  17. void thread_initial(signal_type *signal, unsigned num_invocations)
  18. {
  19. unsigned i;
  20. for(i = 0; i < num_invocations; ++i)
  21. {
  22. (*signal)();
  23. }
  24. }
  25. int main(int argc, const char **argv)
  26. {
  27. if(argc < 3)
  28. {
  29. std::cerr << "usage: " << argv[0] << " <num threads> <num connections>" << std::endl;
  30. return -1;
  31. }
  32. static const unsigned num_threads = std::strtol(argv[1], 0, 0);
  33. static const unsigned num_connections = std::strtol(argv[2], 0, 0);
  34. boost::thread_group threads;
  35. signal_type sig;
  36. std::cout << "Connecting " << num_connections << " connections to signal.\n";
  37. unsigned i;
  38. for(i = 0; i < num_connections; ++i)
  39. {
  40. sig.connect(&myslot);
  41. }
  42. const unsigned num_slot_invocations = 1000000;
  43. const unsigned signal_invocations_per_thread = num_slot_invocations / (num_threads * num_connections);
  44. std::cout << "Launching " << num_threads << " thread(s) to invoke signal " << signal_invocations_per_thread << " times per thread.\n";
  45. for(i = 0; i < num_threads; ++i)
  46. {
  47. threads.create_thread(boost::bind(&thread_initial, &sig, signal_invocations_per_thread));
  48. }
  49. threads.join_all();
  50. return 0;
  51. }