Exemple #1
0
void
time_thread_pool ()
{
    std::cout << "\nTiming how long it takes to launch from thread_pool:\n";
    std::cout << "threads\ttime (best of " << ntrials << ")\n";
    std::cout << "-------\t----------\n";
    thread_pool *pool (default_thread_pool());
    for (int i = 0; threadcounts[i] <= numthreads; ++i) {
        int nt = wedge ? threadcounts[i] : numthreads;
        pool->resize (nt);
        int its = iterations/nt;

        // make a lambda function that spawns a bunch of threads, calls a
        // trivial function, then waits for them to finish and tears down
        // the group.
        auto func = [=](){
            task_set<void> taskset (pool);
            for (int i = 0; i < nt; ++i) {
                taskset.push (pool->push (do_nothing));
            }
            taskset.wait();
        };

        double range;
        double t = time_trial (func, ntrials, its, &range);

        std::cout << Strutil::format ("%2d\t%5.1f   launch %8.1f threads/sec\n",
                                      nt, t, (nt*its)/t);
        if (! wedge)
            break;    // don't loop if we're not wedging
    }
}
Exemple #2
0
void
test_thread_pool_recursion ()
{
    std::cout << "\nTesting thread pool recursion" << std::endl;
    static spin_mutex print_mutex;
    thread_pool *pool (default_thread_pool());
    pool->resize (2);
    parallel_for (0, 10, [&](int id, int64_t i){
        // sleep long enough that we can push all the jobs before any get
        // done.
        Sysutil::usleep (10);
        // then run something else that itself will push jobs onto the
        // thread pool queue.
        parallel_for (0, 10, [&](int id, int64_t i){
            Sysutil::usleep (2);
            spin_lock lock (print_mutex);
            std::cout << "  recursive running thread " << id << std::endl;
        });
    });
}