annotate parallel_processing/ppb_data_shared/ppb_data_shared.cc @ 12:47f90873a72c

add comment
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Fri, 03 Jan 2014 21:11:11 +0900
parents f522b6bf6789
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
1 #include <stdio.h>
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
2 #include <pthread.h>
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
3
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
4 #define THREAD_NUM 2
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
5 #define DATA_NUM 10
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
7 typedef struct _thread_arg {
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
8 int thread_no;
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
9 int *data;
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
10 pthread_mutex_t *mutex;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
11 } thread_arg_t;
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
12
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
13 void *
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
14 thread_func(void *arg)
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
15 {
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
16 thread_arg_t *targ = (thread_arg_t *)arg;
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
17 int result;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
18
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
19 /* starting mutex and ending mutex*/
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
20 for (int i = 0; i < DATA_NUM; i++) {
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
21 pthread_mutex_lock(targ->mutex);
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
22 result = targ->data[i] + 1;
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
23 sched_yield();
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
24 targ->data[i] = result;
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
25 pthread_mutex_unlock(targ->mutex);
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
26 }
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
27 return 0;
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
28 }
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
29
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
30 int
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
31 main()
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
32 {
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
33 pthread_t handle[THREAD_NUM];
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
34 thread_arg_t targ[THREAD_NUM];
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
35 int data[DATA_NUM];
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
36 int i;
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
37 pthread_mutex_t mutex;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
38
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
39 /* initialize */
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
40 for (i = 0; i < DATA_NUM; i++) data[i] = 0;
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
41
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
42 /* initialized mutex*/
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
43 pthread_mutex_init(&mutex, NULL);
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
44
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
45 /* spawn thread a number of THREAD_NUM */
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
46 for (i = 0; i < THREAD_NUM; i++) {
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
47 targ[i].thread_no = i;
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
48 targ[i].data = data;
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
49 targ[i].mutex = &mutex;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
50
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
51 /* spawn thread*/
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
52 pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]);
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
53 }
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
54
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
55 /* wait for running all thread */
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
56 for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL);
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
57
12
47f90873a72c add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
58 /* destroy mutex*/
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
59 pthread_mutex_destroy(&mutex);
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
60
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
61 for (i = 0; i < DATA_NUM; i++) printf("data%d : %d\n", i, data[i]);
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
62 return 0;
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
63 }