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