Mercurial > hg > Members > masakoha > testcode
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 |
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> |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
4 |
14 | 5 #define MAX_THREAD_NUM 2 |
6 #define THREAD_NUM 5 | |
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 | 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 | 18 while (thread_num >= MAX_THREAD_NUM) |
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 | 23 printf("Thread %ld started.\n", id); |
24 sleep(1); | |
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 | 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 | 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 | 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 | 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 } |