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/bind.hpp>
11 #include <boost/signals2.hpp>
12 #include <boost/thread/thread.hpp>
13
14 using namespace boost::placeholders;
15
16 typedef boost::signals2::signal<void ()> signal_type;
17
myslot()18 void myslot()
19 {
20 /* std::cout << __FUNCTION__ << std::endl;
21 sleep(1);*/
22 }
23
thread_initial(signal_type * signal,unsigned num_invocations)24 void thread_initial(signal_type *signal, unsigned num_invocations)
25 {
26 unsigned i;
27 for(i = 0; i < num_invocations; ++i)
28 {
29 (*signal)();
30 }
31 }
32
main(int argc,const char ** argv)33 int main(int argc, const char **argv)
34 {
35 if(argc < 3)
36 {
37 std::cerr << "usage: " << argv[0] << " <num threads> <num connections>" << std::endl;
38 return -1;
39 }
40 static const unsigned num_threads = std::strtol(argv[1], 0, 0);
41 static const unsigned num_connections = std::strtol(argv[2], 0, 0);
42 boost::thread_group threads;
43 signal_type sig;
44
45 std::cout << "Connecting " << num_connections << " connections to signal.\n";
46 unsigned i;
47 for(i = 0; i < num_connections; ++i)
48 {
49 sig.connect(&myslot);
50 }
51 const unsigned num_slot_invocations = 1000000;
52 const unsigned signal_invocations_per_thread = num_slot_invocations / (num_threads * num_connections);
53 std::cout << "Launching " << num_threads << " thread(s) to invoke signal " << signal_invocations_per_thread << " times per thread.\n";
54 for(i = 0; i < num_threads; ++i)
55 {
56 threads.create_thread(boost::bind(&thread_initial, &sig, signal_invocations_per_thread));
57 }
58 threads.join_all();
59 return 0;
60 }
61