Mercurial > hg > Applications > Grep
comparison parallel_processing/ppb_first_thread/ppb_first_thread.cc @ 6:7b0b23f3538d
first pthread programming
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 02 Jan 2014 16:57:28 +0900 |
parents | 4b742b1fedb8 |
children | edf4445da580 |
comparison
equal
deleted
inserted
replaced
5:4b742b1fedb8 | 6:7b0b23f3538d |
---|---|
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 for (i = 0; i < DATA_NUM; i++) data[i] = i; | |
33 | |
34 for (i = 0; i < THREAD_NUM; i++) { | |
35 targ[i].thread_no = i; | |
36 targ[i].data = data; | |
37 | |
38 pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); | |
39 } | |
40 | |
41 for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); | |
42 return 0; | |
43 } |