示例#1
0
文件: async.c 项目: L0op/qemu
ThreadPool *aio_get_thread_pool(AioContext *ctx)
{
    if (!ctx->thread_pool) {
        ctx->thread_pool = thread_pool_new(ctx);
    }
    return ctx->thread_pool;
}
示例#2
0
/**
 * \brief Entry point of the program.
 * \param argc number of arguments.
 * \param argv array of arguments.
 * \return EXIT_SUCCESS or EXIT_FAILURE.
 */
int main(int argc, char** argv)
{
  const size_t tasks_size = 20;
  thread_pool th = NULL;
  struct thread_pool_task tasks[tasks_size];

  (void)argc;
  (void)argv;

  fprintf(stdout, "Begin\n");
  th = thread_pool_new(10);
  fprintf(stdout, "Thread pool: %p\n", (void*)th);

  if(!th)
  {
    fprintf(stderr, "Failed to create pool errno=%d\n",
        errno);
    exit(EXIT_FAILURE);
  }

  for(unsigned int i = 0 ; i < tasks_size ; i++)
  {
    tasks[i].data = (void*)(uintptr_t)i;
    tasks[i].run = fcn_run;
    tasks[i].cleanup = fcn_cleanup;

    if(thread_pool_push(th, &tasks[i]) != 0)
    {
      fprintf(stderr, "Failed to add task %u\n", i);
    }
  }

  thread_pool_start(th);
  sleep(1);

  fprintf(stdout, "Stop stuff\n");
  thread_pool_stop(th);
  fprintf(stdout, "Free stuff\n");
  thread_pool_free(&th);
  fprintf(stdout, "OK\n");

  fprintf(stdout, "End\n");

  return EXIT_SUCCESS;
}
示例#3
0
static int
run_test(int nthreads)
{
    struct benchmark_data * bdata = start_benchmark();
    struct thread_pool * threadpool = thread_pool_new(nthreads);
   
    struct arg2 args = {
        .a = 20,
        .b = 22,
    };

    struct future * sum = thread_pool_submit(threadpool, (fork_join_task_t) adder_task, &args);

    uintptr_t ssum = (uintptr_t) future_get(sum);
    future_free(sum);
    thread_pool_shutdown_and_destroy(threadpool);

    stop_benchmark(bdata);

    // consistency check
    if (ssum != 42) {
        fprintf(stderr, "Wrong result, expected 42, got %ld\n", ssum);
        abort();
    }

    report_benchmark_results(bdata);
    printf("Test successful.\n");
    free(bdata);
    return 0;
}

/**********************************************************************************/

static void
usage(char *av0, int exvalue)
{
    fprintf(stderr, "Usage: %s [-n <n>]\n"
                    " -n number of threads in pool, default %d\n"
                    , av0, DEFAULT_THREADS);
    exit(exvalue);
}
示例#4
0
ThreadPool *
fixed_index_make_thread_pool (guint thread_num)
{
    return thread_pool_new(thread_num,
                           fixed_index_multithreaded_multiphrase_get_thread_func);
}
示例#5
0
文件: gurls.c 项目: jdegges/gurls
int gurls( FILE* fin, uint32_t threads )
{
    struct thread_pool *pool;
    uint32_t i;
    char url[1024];
    char saftey_ext[5] = "txt\0";

    /* validate threads */
    threads = threads > 30 ? 30 :
              threads == 0 ? 5  :
              threads;

    pool = thread_pool_new( threads );
    if( !pool ) {
        return -1;
    }

    /* initialize curl */
    curl_global_init( CURL_GLOBAL_ALL );

    for( i = 0; fgets(url, 1024, fin) ; i++ )
    {
        gurl_url_t* u;
        char* aux;

        if( *url == '\n' || *url == '\0' ) {
            break;
        }

        if( !is_url(url) ) {
            i--;
            continue;
        }

        /* create url object */
        u = malloc( sizeof(gurl_url_t) );
        if( !u ) {
            break;
        }

        /* remove trailing newline */
        for( aux = url; aux-url < 1024 && *aux != '\n' && *aux != '\0'; aux++ );
        *aux = '\0';

        /* fill up url object */
        strncpy( u->url, url, 1024 );
        aux = get_extension( url );
        if( !aux )
            aux = saftey_ext;
        snprintf( u->filename, 1024, "file-%010d.%s", i, aux );

        thread_pool_push( pool, gurl_url_download, u );
    }

    /* send term signal to children */
    thread_pool_terminate( pool );

    /* cleanup */
    thread_pool_free( pool );
    curl_global_cleanup();
    return 0;
}