view parallel_processing/ppb_data_split/ppb_data_split.cc @ 9:fe8c1c25296a

fix ppb_data_split
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Fri, 03 Jan 2014 01:53:39 +0900
parents 645b93cbf36c
children
line wrap: on
line source

#include <stdio.h>
#include <pthread.h>

#define THREAD_NUM 2
#define DATA_NUM 10
#define SPLIT_DATA_NUM (DATA_NUM / THREAD_NUM)

typedef struct _thread_arg {
    int thread_no;
    int *data;
} thread_arg_t;

void *
thread_func(void *arg)
{
    thread_arg_t *targ = (thread_arg_t *)arg;

    for (int i = 0; i < SPLIT_DATA_NUM; i++) {
        printf("thread%d : %d + 1 = %d\n",
            targ->thread_no, targ->data[i], targ->data[i] + 1);
    }
    return 0;
}

int
main()
{
    pthread_t handle[THREAD_NUM];
    thread_arg_t targ[THREAD_NUM];
    int data[DATA_NUM];
    int i;

    /* initialize */
    for (i = 0; i < DATA_NUM; i++) data[i] = i;

    /* spawn thread a number of THREAD_NUM */
    for (i = 0; i < THREAD_NUM; i++) {
        targ[i].thread_no = i;

        /* divide a data into THREAD_NUM*/
        targ[i].data = &data[SPLIT_DATA_NUM * i];
        /* spawn thread*/
        pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]);
    }

    /* wait for running all thread */
    for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL);
    return 0;
}