Mercurial > hg > Applications > Grep
comparison 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 |
comparison
equal
deleted
inserted
replaced
8:645b93cbf36c | 9:fe8c1c25296a |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <pthread.h> | 2 #include <pthread.h> |
3 | 3 |
4 #define THREAD_NUM 2 | 4 #define THREAD_NUM 2 |
5 #define DATA_NUM 10 | 5 #define DATA_NUM 10 |
6 #define SPLIT_DATA_NUM (DATA_NUM / THREAD_NUM) | |
6 | 7 |
7 typedef struct _thread_arg { | 8 typedef struct _thread_arg { |
8 int thread_no; | 9 int thread_no; |
9 int *data; | 10 int *data; |
10 } thread_arg_t; | 11 } thread_arg_t; |
12 void * | 13 void * |
13 thread_func(void *arg) | 14 thread_func(void *arg) |
14 { | 15 { |
15 thread_arg_t *targ = (thread_arg_t *)arg; | 16 thread_arg_t *targ = (thread_arg_t *)arg; |
16 | 17 |
17 for (int i = 0; i < DATA_NUM; i++) { | 18 for (int i = 0; i < SPLIT_DATA_NUM; i++) { |
18 printf("thread%d : %d + 1 = %d\n", | 19 printf("thread%d : %d + 1 = %d\n", |
19 targ->thread_no, targ->data[i], targ->data[i] + 1); | 20 targ->thread_no, targ->data[i], targ->data[i] + 1); |
20 } | 21 } |
21 return 0; | 22 return 0; |
22 } | 23 } |
33 for (i = 0; i < DATA_NUM; i++) data[i] = i; | 34 for (i = 0; i < DATA_NUM; i++) data[i] = i; |
34 | 35 |
35 /* spawn thread a number of THREAD_NUM */ | 36 /* spawn thread a number of THREAD_NUM */ |
36 for (i = 0; i < THREAD_NUM; i++) { | 37 for (i = 0; i < THREAD_NUM; i++) { |
37 targ[i].thread_no = i; | 38 targ[i].thread_no = i; |
38 targ[i].data = data; | |
39 | 39 |
40 /* divide a data into THREAD_NUM*/ | |
41 targ[i].data = &data[SPLIT_DATA_NUM * i]; | |
40 /* spawn thread*/ | 42 /* spawn thread*/ |
41 pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); | 43 pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); |
42 } | 44 } |
43 | 45 |
44 /* wait for running all thread */ | 46 /* wait for running all thread */ |