1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
//#include <thread>
#include <vector>
#include <boost/thread.hpp>
#include <zypp/base/Functional.h>
#include <zypp/base/Easy.h>
#include <zypp/base/Measure.h>
#include <zypp/base/Exception.h>
#include <zypp/base/String.h>
#include <zypp/base/LogTools.h>
#include <zypp/base/LogControl.h>
#include <zypp/ExternalProgram.h>
using std::endl;
using zypp::debug::Measure;
///////////////////////////////////////////////////////////////////
namespace zypp
{
/** Run a number of tasks using \c threadCount threads.
*
* \code
* std::vector<function<void()>> tasks;
* for ( int i = 0; i < 100; ++i )
* {
* tasks.push_back( [i]()
* {
* MIL << '[' << i << "]" << endl;
* });
* }
* runTasks( tasks, 10 );
* \endcode
*/
template <class TFunction>
void runTasks( const std::vector<TFunction>& tasks, size_t threadCount = 1 )
{
if ( threadCount )
{
boost::thread_group group;
const size_t taskCount = (tasks.size() / threadCount) + 1;
for ( size_t start = 0; start < tasks.size(); start += taskCount )
{
group.create_thread( [&tasks, start, taskCount]()
{
const size_t end = std::min( tasks.size(), start + taskCount );
for ( size_t i = start; i < end; ++i )
tasks[i]();
});
}
group.join_all();
}
else
{
for_( f, tasks.begin(), tasks.end() )
(*f)();
}
}
}
///////////////////////////////////////////////////////////////////
using namespace zypp;
int main( int argc, char * argv[] )
try {
--argc;
++argv;
zypp::base::LogControl::instance().logToStdErr();
INT << "===[START]==========================================" << endl;
///////////////////////////////////////////////////////////////////
std::vector<function<void()>> tasks;
for ( int i = 0; i < 100; ++i )
{
tasks.push_back( [i]()
{
MIL << '[' << i << "]" << endl;
});
}
{
Measure x( "THREAD" );
runTasks( tasks, 100 );
}
///////////////////////////////////////////////////////////////////
INT << "===[END]============================================" << endl << endl;
zypp::base::LogControl::instance().logNothing();
return 0;
}
catch ( const zypp::Exception & exp )
{
INT << exp << endl << exp.historyAsString();
}
catch (...)
{}
|