编程辅导 CS3214 Fall 2014.

* Fork/Join Framework
* Tests simple recursive join.
* Written by G. Back for CS3214 Fall 2014.
#include

Copyright By PowCoder代写 加微信 powcoder

#include
#include
#include
#include
#include
#include
#include
#include
#include

#include “threadpool.h”
#include “threadpool_lib.h”
#define DEFAULT_THREADS 1

/* Data to be passed to callable. */
struct arg2 {
uintptr_t a;
uintptr_t b;

* A FJ task that multiplies 2 numbers.
static void *
multiplier_task(struct thread_pool *pool, struct arg2 * data)
return (void *)(data->a * data->b);

* A FJ task that adds 2 numbers.
static void *
adder_task(struct thread_pool *pool, struct arg2 * data)
return (void *)(data->a + data->b);

static void *
test_task(struct thread_pool *pool, struct arg2 * data)
struct future *f1 = thread_pool_submit(pool, (fork_join_task_t) adder_task, data);
uintptr_t r1 = (uintptr_t) future_get(f1);
future_free(f1);

struct arg2 a2 = {
struct future *f2 = thread_pool_submit(pool, (fork_join_task_t) multiplier_task, &a2);
uintptr_t r2 = (uintptr_t) future_get(f2);
future_free(f2);

return (void *)r2;

static int
run_test(int nthreads)
struct benchmark_data * bdata = start_benchmark();
struct thread_pool * threadpool = thread_pool_new(nthreads);

struct arg2 args = {

struct future * sum = thread_pool_submit(threadpool, (fork_join_task_t) test_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);

report_benchmark_results(bdata);
printf(“Test successful.\n”);
free(bdata);

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

static void
usage(char *av0, int exvalue)
fprintf(stderr, “Usage: %s [-n ]\n”
” -n number of threads in pool, default %d\n”
, av0, DEFAULT_THREADS);
exit(exvalue);

main(int ac, char *av[])
int c, nthreads = DEFAULT_THREADS;
while ((c = getopt(ac, av, “hn:”)) != EOF) {
switch (c) {
nthreads = atoi(optarg);
usage(av[0], EXIT_SUCCESS);

return run_test(nthreads);

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com