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
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>
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
3 #include <unistd.h>
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
4 #include <semaphore.h>
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
5
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
6 #define MAX_THREAD_NUM 2
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
7 #define THREAD_NUM 5
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
8
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
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
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
14 long id = (long)arg;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
15
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
16 /* starting semaphore and ending semaphore*/
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
17 sem_wait(&sem);
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
18 printf("Thread %ld started.\n", id);
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
19 sleep(1);
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
20 printf("Thread %ld finished.\n", id);
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
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
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
29 long i;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
30
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
31 /* initialize */
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
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
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
34 /* spawn thread a number of THREAD_NUM */
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
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
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
37 /* wait for running all thread */
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
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
47f90873a72c add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
40 /* destroy mutex*/
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
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 }