Mercurial > hg > Members > masakoha > testcode
changeset 10:51177cc6ab38
add ppb_data_shared
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 03 Jan 2014 02:03:11 +0900 |
parents | fe8c1c25296a |
children | f522b6bf6789 |
files | parallel_processing/ppb_data_shared/Makefile parallel_processing/ppb_data_shared/ppb_data_shared.cc |
diffstat | 2 files changed, 60 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/ppb_data_shared/Makefile Fri Jan 03 02:03:11 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_data_shared +CC = clang++ +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).cc + +clean: + rm -f $(TARGET) + rm -f *.o
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/ppb_data_shared/ppb_data_shared.cc Fri Jan 03 02:03:11 2014 +0900 @@ -0,0 +1,49 @@ +#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; +}