annotate parallel_processing/ppb_cond_queue/ppb_cond_queue.c @ 19:a9534f217a0c

add some files
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Mon, 06 Jan 2014 17:50:56 +0900
parents parallel_processing/ppb_cond_counter/ppb_cond_counter.c@7efe4455deaa
children d137f1823794
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>
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
4
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
5 #define MAX_THREAD_NUM 2
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
6 #define THREAD_NUM 5
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
7
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
8 pthread_mutex_t mutex;
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
9 pthread_cond_t cond;
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
10 int thread_num = 0;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
11
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
12 void *
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
13 thread_func(void *arg)
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
14 {
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
15 long id = (long)arg;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
16
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
17 pthread_mutex_lock(&mutex);
18
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
18 while (thread_num >= MAX_THREAD_NUM)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
19 pthread_cond_wait(&cond, &mutex);
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
20 thread_num++;
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
21 pthread_mutex_unlock(&mutex);
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
22
14
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
23 printf("Thread %ld started.\n", id);
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
24 sleep(1);
cc2e4f2c9ce8 some fix
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
25 printf("Thread %ld finished.\n", id);
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
26
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
27 pthread_mutex_lock(&mutex);
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
28 thread_num--;
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
29 pthread_cond_signal(&cond);
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
30 pthread_mutex_unlock(&mutex);
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
31
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
32 return 0;
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
33 }
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
34
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
35 int
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
36 main()
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
37 {
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
38 long i;
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
39 pthread_t handle[THREAD_NUM];
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
40
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
41 /* initialize */
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
42 pthread_mutex_init(&mutex, NULL);
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
43 pthread_cond_init(&cond, 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 */
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
46 for (i = 0; i < THREAD_NUM; ++i)
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
47 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
48
7
edf4445da580 add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
49 /* wait for running all thread */
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
50 for (i = 0; i < THREAD_NUM; ++i)
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
51 pthread_join(handle[i], NULL);
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
52
12
47f90873a72c add comment
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
53 /* destroy mutex*/
17
4d8d124528f2 fix ppb_cond_counter
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
54 pthread_cond_destroy(&cond);
11
f522b6bf6789 fix ppb_data_shared
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 10
diff changeset
55
6
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
56 return 0;
7b0b23f3538d first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
57 }