3 #include <boost/thread.hpp>
4 #include <zypp/base/Functional.h>
6 #include <zypp/base/Easy.h>
7 #include <zypp/base/Measure.h>
8 #include <zypp/base/Exception.h>
9 #include <zypp/base/String.h>
11 #include <zypp/base/LogTools.h>
12 #include <zypp/base/LogControl.h>
14 #include "zypp/ExternalProgram.h"
17 using zypp::debug::Measure;
19 ///////////////////////////////////////////////////////////////////
22 /** Run a number of tasks using \c threadCount threads.
25 * std::vector<function<void()>> tasks;
26 * for ( int i = 0; i < 100; ++i )
28 * tasks.push_back( [i]()
30 * MIL << '[' << i << "]" << endl;
33 * runTasks( tasks, 10 );
36 template <class _Function>
37 void runTasks( const std::vector<_Function>& tasks, size_t threadCount = 1 )
41 boost::thread_group group;
42 const size_t taskCount = (tasks.size() / threadCount) + 1;
43 for ( size_t start = 0; start < tasks.size(); start += taskCount )
45 group.create_thread( [&tasks, start, taskCount]()
47 const size_t end = std::min( tasks.size(), start + taskCount );
48 for ( size_t i = start; i < end; ++i )
56 for_( f, tasks.begin(), tasks.end() )
61 ///////////////////////////////////////////////////////////////////
63 int main( int argc, char * argv[] )
67 zypp::base::LogControl::instance().logToStdErr();
68 INT << "===[START]==========================================" << endl;
69 ///////////////////////////////////////////////////////////////////
71 std::vector<function<void()>> tasks;
72 for ( int i = 0; i < 100; ++i )
74 tasks.push_back( [i]()
76 MIL << '[' << i << "]" << endl;
80 Measure x( "THREAD" );
81 runTasks( tasks, 100 );
85 ///////////////////////////////////////////////////////////////////
86 INT << "===[END]============================================" << endl << endl;
87 zypp::base::LogControl::instance().logNothing();
90 catch ( const zypp::Exception & exp )
92 INT << exp << endl << exp.historyAsString();