⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cc

📁 线程池 线程池 线程池
💻 CC
字号:
#include <stdlib.h>#include <cmath>#include "TThreadPool.hh"#include "TTimer.hh"#include "TRNG.hh"//// for some benchmarking//static uint job_number = 0;class TBenchJob : public TThreadPool::TJob{protected:    uint   _size;    public:    TBenchJob ( int i, uint s ) : TThreadPool::TJob( i ), _size(s) {}    virtual void run ( void * )    {        //cout << "  starting thread (" << job_number++ << "), size = " << _size << endl;                real * _matrix = new real[ _size * _size ];                for ( uint i = 0; i < _size; i++ )        {            for ( uint j = 0; j < _size; j++ )            {                _matrix[ (i*_size) + j ] = std::sin( real(j) * M_PI * std::cos( real(i) ));            }// for        }// for        delete _matrix;    }};#define MAX_SIZE  1000#define MAX_RAND  500voidrecursion ( uint level, TRNG & rng ){    if ( level == 0 )    {        TBenchJob * job = new TBenchJob( -1, int(rng.rand( MAX_RAND )) + MAX_SIZE );        thread_pool->run( job );    }// if    else    {        recursion( level-1, rng );        recursion( level-1, rng );        recursion( level-1, rng );        recursion( level-1, rng );    }// else}voidbench1 ( int argc, char ** argv ){    TRNG  rng;    uint  i = 1;    uint  thr_count = 16;    uint  rec_depth = 6;        if ( argc > 1 ) thr_count = atoi( argv[1] );    if ( argc > 2 ) rec_depth = atoi( argv[2] );    for ( uint j = 0; j < rec_depth; j++ )        i *= 4;        init_thread_pool( thr_count );    cout << "executing " << i << " jobs using " << thr_count << " thread(s)" << endl;        TTimer  timer( REAL_TIME );    timer.start();        recursion( rec_depth, rng );        thread_pool->sync_all();    timer.stop();    cout << "time for recursion = " << timer << endl;}class TBench2Job : public TThreadPool::TJob{public:    TBench2Job ( int i ) : TThreadPool::TJob( i ) {}    virtual void run ( void * )    {        // do nothing    }};class TBench2Thr : public TThread{public:    TBench2Thr ( int i ) : TThread( i ) {}    virtual void run ()    {        // do nothing    }};voidbench2 ( int argc, char ** argv ){    int  max_jobs = 1000000;    init_thread_pool( 1 );    TTimer  timer( REAL_TIME );    timer.start();        for ( uint i = 0; i  < max_jobs; i++ )    {        TBench2Job  * job = new TBench2Job( i );        delete job;    }    timer.stop();    cout << "time to create jobs = " << timer << endl;    timer.start();        for ( uint i = 0; i  < max_jobs; i++ )    {        TBench2Thr  * job = new TBench2Thr( i );        delete job;    }    timer.stop();    cout << "time to create threads = " << timer << endl;    timer.start();        for ( uint i = 0; i  < max_jobs; i++ )    {        TBench2Job  * job = new TBench2Job( i );        thread_pool->run( job );        thread_pool->sync( job );        delete job;    }    timer.stop();    cout << "time for thread pool = " << timer << endl;    timer.start();        for ( uint i = 0; i  < max_jobs; i++ )    {        TBench2Thr  * job = new TBench2Thr( i );        job->start();        job->join();                delete job;    }    timer.stop();    cout << "time for lwp-threads = " << timer << endl;    timer.start();        for ( uint i = 0; i  < max_jobs; i++ )    {        TBench2Thr  * job = new TBench2Thr( i );        job->start( false, true );        job->join();                delete job;    }    timer.stop();    cout << "time for hwp-threads = " << timer << endl;}intmain ( int argc, char ** argv ){    // bench1( argc, argv );    bench2( argc, argv );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -