Mercurial > hg > Members > masakoha > testcode
annotate parallel_processing/ppb_sem_counter/ppb_sem_counter.cc @ 14:cc2e4f2c9ce8
some fix
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 06 Jan 2014 14:36:04 +0900 (2014-01-06) |
parents | 753289c70eb4 |
children | 08beb7bff036 |
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> |
14 | 3 #include <unistd.h> |
4 #include <semaphore.h> | |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
5 |
14 | 6 #define MAX_THREAD_NUM 2 |
7 #define THREAD_NUM 5 | |
8 | |
9 sem_t sem; | |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
10 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
11 void * |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
12 thread_func(void *arg) |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
13 { |
14 | 14 long id = (long)arg; |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
15 |
14 | 16 /* starting semaphore and ending semaphore*/ |
17 sem_wait(&sem); | |
18 printf("Thread %ld started.\n", id); | |
19 sleep(1); | |
20 printf("Thread %ld finished.\n", id); | |
21 sem_post(&sem); | |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
22 return 0; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
23 } |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
24 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
25 int |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
26 main() |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
27 { |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
28 pthread_t handle[THREAD_NUM]; |
14 | 29 long i; |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
30 |
7 | 31 /* initialize */ |
14 | 32 sem_init(&sem, 0, MAX_THREAD_NUM); |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
33 |
7 | 34 /* spawn thread a number of THREAD_NUM */ |
14 | 35 for (i = 0; i < THREAD_NUM; ++i) pthread_create(&handle[i], NULL, &thread_func, (void*)i); |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
36 |
7 | 37 /* wait for running all thread */ |
14 | 38 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
|
39 |
12 | 40 /* destroy mutex*/ |
14 | 41 sem_destroy(&sem); |
11
f522b6bf6789
fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
42 |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
43 return 0; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
44 } |