Mercurial > hg > Applications > Grep
comparison parallel_processing/chapter3/ppb_first_thread/ppb_first_thread.cc @ 22:508b47c8f4d8
package Chapter3
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 07 Jan 2014 14:30:28 +0900 |
parents | parallel_processing/ppb_first_thread/ppb_first_thread.cc@edf4445da580 |
children |
comparison
equal
deleted
inserted
replaced
21:d137f1823794 | 22:508b47c8f4d8 |
---|---|
1 #include <stdio.h> | |
2 #include <pthread.h> | |
3 | |
4 #define THREAD_NUM 2 | |
5 #define DATA_NUM 10 | |
6 | |
7 typedef struct _thread_arg { | |
8 int thread_no; | |
9 int *data; | |
10 } thread_arg_t; | |
11 | |
12 void * | |
13 thread_func(void *arg) | |
14 { | |
15 thread_arg_t *targ = (thread_arg_t *)arg; | |
16 | |
17 for (int i = 0; i < DATA_NUM; i++) { | |
18 printf("thread%d : %d + 1 = %d\n", | |
19 targ->thread_no, targ->data[i], targ->data[i] + 1); | |
20 } | |
21 return 0; | |
22 } | |
23 | |
24 int | |
25 main() | |
26 { | |
27 pthread_t handle[THREAD_NUM]; | |
28 thread_arg_t targ[THREAD_NUM]; | |
29 int data[DATA_NUM]; | |
30 int i; | |
31 | |
32 /* initialize */ | |
33 for (i = 0; i < DATA_NUM; i++) data[i] = i; | |
34 | |
35 /* spawn thread a number of THREAD_NUM */ | |
36 for (i = 0; i < THREAD_NUM; i++) { | |
37 targ[i].thread_no = i; | |
38 targ[i].data = data; | |
39 | |
40 /* spawn thread*/ | |
41 pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); | |
42 } | |
43 | |
44 /* wait for running all thread */ | |
45 for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); | |
46 return 0; | |
47 } |