# HG changeset patch # User Masataka Kohagura # Date 1389072628 -32400 # Node ID 508b47c8f4d875bb5f0e23d2a22b71c69fbecf0d # Parent d137f1823794577fa4bdd5a8c814fa5d7f93370d package Chapter3 diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_cond_counter/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_cond_counter/Makefile Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_cond_counter +CC = clang +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).c + +clean: + rm -f $(TARGET) + rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_cond_counter/ppb_cond_counter.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_cond_counter/ppb_cond_counter.c Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,57 @@ +#include +#include +#include + +#define MAX_THREAD_NUM 2 +#define THREAD_NUM 5 + +pthread_mutex_t mutex; +pthread_cond_t cond; +int thread_num = 0; + +void * +thread_func(void *arg) +{ + long id = (long)arg; + + pthread_mutex_lock(&mutex); + while (thread_num >= MAX_THREAD_NUM) + pthread_cond_wait(&cond, &mutex); + thread_num++; + pthread_mutex_unlock(&mutex); + + printf("Thread %ld started.\n", id); + sleep(1); + printf("Thread %ld finished.\n", id); + + pthread_mutex_lock(&mutex); + thread_num--; + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + + return 0; +} + +int +main() +{ + long i; + pthread_t handle[THREAD_NUM]; + + /* initialize */ + pthread_mutex_init(&mutex, NULL); + pthread_cond_init(&cond, NULL); + + /* spawn thread a number of THREAD_NUM */ + for (i = 0; i < THREAD_NUM; ++i) + pthread_create(&handle[i], NULL, &thread_func, (void*)i); + + /* wait for running all thread */ + for (i = 0; i < THREAD_NUM; ++i) + pthread_join(handle[i], NULL); + + /* destroy mutex*/ + pthread_cond_destroy(&cond); + + return 0; +} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_cond_queue/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_cond_queue/Makefile Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_cond_queue +CC = clang +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).c + +clean: + rm -f $(TARGET) + rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_cond_queue/ppb_cond_queue.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_cond_queue/ppb_cond_queue.c Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include "ppb_queue.h" + +#define THREAD_NUM 2 +#define DATA_NUM 10 +#define MAX_QUEUE_NUM 3 +#define THREAD_DATA_NUM (DATA_NUM / THREAD_NUM) + +extern void enqueue(queue_t, int); +extern void dequeue(queue_t, int); + +typedef struct _queue { + int values[MAX_QUEUE_NUM]; + volatile int remain; + int rp, wp; + pthread_mutex_t mutex; + pthread_cond_t not_full; + pthread_cond_t not_empty; +} queue_t; + +typedef struct _thread_arg { + int id; + queue_t *queue; +} thread_arg_t; + +void * +producer_func(void *arg) +{ + thread_arg_t *targ = (thread_arg_t*)arg; + + for (int i = 0; i < THREAD_DATA_NUM; i++) { + int num = targ->id * THREAD_DATA_NUM + 1; + enqueue(targ->queue, num); + printf("[Producer %d] ==> %d \n", targ->id, num); + sleep(rand() % 3); + } + enqueue(targ->queue, END_DATA); + return 0; +} + +void * +consumer_func(void *arg) +{ + thread_arg_t *targ = (thread_arg_t*)arg; + int i; + while (1) { + dequeue(targ->queue, &i); + if (i == END_DATA) break; + printf("[Consumer %d] ==> %d \n", targ->id, i); + sleep(rand() % 3); + } + return 0; +} + +int +main() +{ + pthread_t producer[THREAD_NUM], consumer[THREAD_NUM]; + thread_arg_t ptarg[THREAD_NUM], ctarg[THREAD_NUM]; + queue_t queue; + int i; + + /* initialize */ + queue.rp = queue.wp = 0; + queue.remain = 0; + pthread_mutex_init(&queue.mutex, NULL); + pthread_cond_init(&queue.not_full, NULL); + pthread_cond_init(&queue.not_empty, NULL); + + /* spawn Producer thread */ + for (i = 0; i < THREAD_NUM; ++i) { + ptarg[i].id = i; + ptarg[i].queue = &queue; + pthread_create(&consumer[i], NULL, &producer_func, &ptarg[i]); + } + + /* spawn Consumer thread */ + for (i = 0; i < THREAD_NUM; ++i) { + ctarg[i].id = i; + ctarg[i].queue = &queue; + pthread_create(&consumer[i], NULL, &consumer_func, &ctarg[i]); + } + + /* wait for running all thread */ + for (i = 0; i < THREAD_NUM; ++i) + pthread_join(consumer[i], NULL); + + return 0; +} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_cond_queue/ppb_queue.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_cond_queue/ppb_queue.h Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,43 @@ +#ifdef _PPB_QUEUE_H +#define _PPB_QUEUE_H +#include +#include + +#ifdef MAX_QUEUE_NUM +#define MAX_QUEUE_NUM 10 +#endif /* MAX_QUEUE_NUM */ + +/* +typedef struct _queue { + int values[MAX_QUEUE_NUM]; + volatile int remain; + int rp, wp; + pthread_mutex_t mutex; + pthread_cond_t not_full; + pthread_cond_t not_empty; +} queue_t; +*/ + +void enqueue(queue_t *q, int v) { + pthread_mutex_lock(&q->mutex); + while (q->remain == MAX_QUEUE_NUM) + pthread_cond_wait(&q->not_full, &q->mutex); + q->values[q->wp] = v; + q->wp++; q->remain++; + if (q->wp == MAX_QUEUE_NUM) q->wp = 0; + pthread_cond_signal(&q->not_empty); + pthread_mutex_unlock(&q->mutex); +} + +void dequeue(queue_t *q, int v) { + pthread_mutex_lock(&q->mutex); + while (q->remain == 0) + pthread_cond_wait(&q->not_empty, &q->mutex); + v = q->values[q->rp]; + q->rp++; q->remain--; + if (q->rp == MAX_QUEUE_NUM) q->rp = 0; + pthread_cond_signal(&q->not_full); + pthread_mutex_unlock(&q->mutex); +} + +#endif /* _PPB_QUEUE_H */ diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_data_shared/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_data_shared/Makefile Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_data_shared +CC = clang++ +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).cc + +clean: + rm -f $(TARGET) + rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_data_shared/ppb_data_shared.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_data_shared/ppb_data_shared.cc Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,63 @@ +#include +#include + +#define THREAD_NUM 2 +#define DATA_NUM 10 + +typedef struct _thread_arg { + int thread_no; + int *data; + pthread_mutex_t *mutex; +} thread_arg_t; + +void * +thread_func(void *arg) +{ + thread_arg_t *targ = (thread_arg_t *)arg; + int result; + + /* starting mutex and ending mutex*/ + for (int i = 0; i < DATA_NUM; i++) { + pthread_mutex_lock(targ->mutex); + result = targ->data[i] + 1; + sched_yield(); + targ->data[i] = result; + pthread_mutex_unlock(targ->mutex); + } + return 0; +} + +int +main() +{ + pthread_t handle[THREAD_NUM]; + thread_arg_t targ[THREAD_NUM]; + int data[DATA_NUM]; + int i; + pthread_mutex_t mutex; + + /* initialize */ + for (i = 0; i < DATA_NUM; i++) data[i] = 0; + + /* initialized mutex*/ + pthread_mutex_init(&mutex, NULL); + + /* spawn thread a number of THREAD_NUM */ + for (i = 0; i < THREAD_NUM; i++) { + targ[i].thread_no = i; + targ[i].data = data; + targ[i].mutex = &mutex; + + /* spawn thread*/ + pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); + } + + /* wait for running all thread */ + for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); + + /* destroy mutex*/ + pthread_mutex_destroy(&mutex); + + for (i = 0; i < DATA_NUM; i++) printf("data%d : %d\n", i, data[i]); + return 0; +} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_data_split/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_data_split/Makefile Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_data_split +CC = clang++ +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).cc + +clean: + rm -f $(TARGET) + rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_data_split/ppb_data_split.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_data_split/ppb_data_split.cc Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,49 @@ +#include +#include + +#define THREAD_NUM 2 +#define DATA_NUM 10 +#define SPLIT_DATA_NUM (DATA_NUM / THREAD_NUM) + +typedef struct _thread_arg { + int thread_no; + int *data; +} thread_arg_t; + +void * +thread_func(void *arg) +{ + thread_arg_t *targ = (thread_arg_t *)arg; + + for (int i = 0; i < SPLIT_DATA_NUM; i++) { + printf("thread%d : %d + 1 = %d\n", + targ->thread_no, targ->data[i], targ->data[i] + 1); + } + return 0; +} + +int +main() +{ + pthread_t handle[THREAD_NUM]; + thread_arg_t targ[THREAD_NUM]; + int data[DATA_NUM]; + int i; + + /* initialize */ + for (i = 0; i < DATA_NUM; i++) data[i] = i; + + /* spawn thread a number of THREAD_NUM */ + for (i = 0; i < THREAD_NUM; i++) { + targ[i].thread_no = i; + + /* divide a data into THREAD_NUM*/ + targ[i].data = &data[SPLIT_DATA_NUM * i]; + /* spawn thread*/ + pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); + } + + /* wait for running all thread */ + for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); + return 0; +} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_first_thread/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_first_thread/Makefile Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_first_thread +CC = clang++ +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).cc + +clean: + rm -f $(TARGET) + rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_first_thread/ppb_first_thread.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_first_thread/ppb_first_thread.cc Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,47 @@ +#include +#include + +#define THREAD_NUM 2 +#define DATA_NUM 10 + +typedef struct _thread_arg { + int thread_no; + int *data; +} thread_arg_t; + +void * +thread_func(void *arg) +{ + thread_arg_t *targ = (thread_arg_t *)arg; + + for (int i = 0; i < DATA_NUM; i++) { + printf("thread%d : %d + 1 = %d\n", + targ->thread_no, targ->data[i], targ->data[i] + 1); + } + return 0; +} + +int +main() +{ + pthread_t handle[THREAD_NUM]; + thread_arg_t targ[THREAD_NUM]; + int data[DATA_NUM]; + int i; + + /* initialize */ + for (i = 0; i < DATA_NUM; i++) data[i] = i; + + /* spawn thread a number of THREAD_NUM */ + for (i = 0; i < THREAD_NUM; i++) { + targ[i].thread_no = i; + targ[i].data = data; + + /* spawn thread*/ + pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); + } + + /* wait for running all thread */ + for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); + return 0; +} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_sem_counter/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_sem_counter/Makefile Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,11 @@ +TARGET = ppb_sem_counter +CC = clang++ +CPPFLAGS = -g -O0 + +$(TARGET): $(TARGET).o + $(CC) -g -O0 -o $@ $< +$(TARGET).o: $(TARGET).cc + +clean: + rm -f $(TARGET) + rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/chapter3/ppb_sem_counter/ppb_sem_counter.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parallel_processing/chapter3/ppb_sem_counter/ppb_sem_counter.cc Tue Jan 07 14:30:28 2014 +0900 @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#define MAX_THREAD_NUM 2 +#define THREAD_NUM 5 + +sem_t sem; + +/** + * Mavericks では clang が default となっているが、書籍自体は gcc でコンパイルしている。 + * クラスタ環境に gcc が存在していたので、そこでコンパイルすると望ましい動作結果が得られた。 + * clang では・・・どうするんだろ + */ +void * +thread_func(void *arg) +{ + long id = (long)arg; + + /* starting semaphore and ending semaphore*/ + sem_wait(&sem); + printf("Thread %ld started.\n", id); + sleep(1); + printf("Thread %ld finished.\n", id); + sem_post(&sem); + return 0; +} + +int +main() +{ + pthread_t handle[THREAD_NUM]; + long i; + + /* initialize */ + sem_init(&sem, 0, MAX_THREAD_NUM); + + /* spawn thread a number of THREAD_NUM */ + for (i = 0; i < THREAD_NUM; ++i) pthread_create(&handle[i], NULL, &thread_func, (void*)i); + + /* wait for running all thread */ + for (i = 0; i < THREAD_NUM; ++i) pthread_join(handle[i], NULL); + + /* destroy mutex*/ + sem_destroy(&sem); + + return 0; +} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_cond_counter/Makefile --- a/parallel_processing/ppb_cond_counter/Makefile Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -TARGET = ppb_cond_counter -CC = clang -CPPFLAGS = -g -O0 - -$(TARGET): $(TARGET).o - $(CC) -g -O0 -o $@ $< -$(TARGET).o: $(TARGET).c - -clean: - rm -f $(TARGET) - rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_cond_counter/ppb_cond_counter.c --- a/parallel_processing/ppb_cond_counter/ppb_cond_counter.c Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -#include -#include -#include - -#define MAX_THREAD_NUM 2 -#define THREAD_NUM 5 - -pthread_mutex_t mutex; -pthread_cond_t cond; -int thread_num = 0; - -void * -thread_func(void *arg) -{ - long id = (long)arg; - - pthread_mutex_lock(&mutex); - while (thread_num >= MAX_THREAD_NUM) - pthread_cond_wait(&cond, &mutex); - thread_num++; - pthread_mutex_unlock(&mutex); - - printf("Thread %ld started.\n", id); - sleep(1); - printf("Thread %ld finished.\n", id); - - pthread_mutex_lock(&mutex); - thread_num--; - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); - - return 0; -} - -int -main() -{ - long i; - pthread_t handle[THREAD_NUM]; - - /* initialize */ - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(&cond, NULL); - - /* spawn thread a number of THREAD_NUM */ - for (i = 0; i < THREAD_NUM; ++i) - pthread_create(&handle[i], NULL, &thread_func, (void*)i); - - /* wait for running all thread */ - for (i = 0; i < THREAD_NUM; ++i) - pthread_join(handle[i], NULL); - - /* destroy mutex*/ - pthread_cond_destroy(&cond); - - return 0; -} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_cond_queue/Makefile --- a/parallel_processing/ppb_cond_queue/Makefile Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -TARGET = ppb_cond_queue -CC = clang -CPPFLAGS = -g -O0 - -$(TARGET): $(TARGET).o - $(CC) -g -O0 -o $@ $< -$(TARGET).o: $(TARGET).c - -clean: - rm -f $(TARGET) - rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_cond_queue/ppb_cond_queue.c --- a/parallel_processing/ppb_cond_queue/ppb_cond_queue.c Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -#include -#include -#include -#include -#include -#include "ppb_queue.h" - -#define THREAD_NUM 2 -#define DATA_NUM 10 -#define MAX_QUEUE_NUM 3 -#define THREAD_DATA_NUM (DATA_NUM / THREAD_NUM) - -extern void enqueue(queue_t, int); -extern void dequeue(queue_t, int); - -typedef struct _queue { - int values[MAX_QUEUE_NUM]; - volatile int remain; - int rp, wp; - pthread_mutex_t mutex; - pthread_cond_t not_full; - pthread_cond_t not_empty; -} queue_t; - -typedef struct _thread_arg { - int id; - queue_t *queue; -} thread_arg_t; - -void * -producer_func(void *arg) -{ - thread_arg_t *targ = (thread_arg_t*)arg; - - for (int i = 0; i < THREAD_DATA_NUM; i++) { - int num = targ->id * THREAD_DATA_NUM + 1; - enqueue(targ->queue, num); - printf("[Producer %d] ==> %d \n", targ->id, num); - sleep(rand() % 3); - } - enqueue(targ->queue, END_DATA); - return 0; -} - -void * -consumer_func(void *arg) -{ - thread_arg_t *targ = (thread_arg_t*)arg; - int i; - while (1) { - dequeue(targ->queue, &i); - if (i == END_DATA) break; - printf("[Consumer %d] ==> %d \n", targ->id, i); - sleep(rand() % 3); - } - return 0; -} - -int -main() -{ - pthread_t producer[THREAD_NUM], consumer[THREAD_NUM]; - thread_arg_t ptarg[THREAD_NUM], ctarg[THREAD_NUM]; - queue_t queue; - int i; - - /* initialize */ - queue.rp = queue.wp = 0; - queue.remain = 0; - pthread_mutex_init(&queue.mutex, NULL); - pthread_cond_init(&queue.not_full, NULL); - pthread_cond_init(&queue.not_empty, NULL); - - /* spawn Producer thread */ - for (i = 0; i < THREAD_NUM; ++i) { - ptarg[i].id = i; - ptarg[i].queue = &queue; - pthread_create(&consumer[i], NULL, &producer_func, &ptarg[i]); - } - - /* spawn Consumer thread */ - for (i = 0; i < THREAD_NUM; ++i) { - ctarg[i].id = i; - ctarg[i].queue = &queue; - pthread_create(&consumer[i], NULL, &consumer_func, &ctarg[i]); - } - - /* wait for running all thread */ - for (i = 0; i < THREAD_NUM; ++i) - pthread_join(consumer[i], NULL); - - return 0; -} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_cond_queue/ppb_queue.h --- a/parallel_processing/ppb_cond_queue/ppb_queue.h Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -#ifdef _PPB_QUEUE_H -#define _PPB_QUEUE_H -#include -#include - -#ifdef MAX_QUEUE_NUM -#define MAX_QUEUE_NUM 10 -#endif /* MAX_QUEUE_NUM */ - -/* -typedef struct _queue { - int values[MAX_QUEUE_NUM]; - volatile int remain; - int rp, wp; - pthread_mutex_t mutex; - pthread_cond_t not_full; - pthread_cond_t not_empty; -} queue_t; -*/ - -void enqueue(queue_t *q, int v) { - pthread_mutex_lock(&q->mutex); - while (q->remain == MAX_QUEUE_NUM) - pthread_cond_wait(&q->not_full, &q->mutex); - q->values[q->wp] = v; - q->wp++; q->remain++; - if (q->wp == MAX_QUEUE_NUM) q->wp = 0; - pthread_cond_signal(&q->not_empty); - pthread_mutex_unlock(&q->mutex); -} - -void dequeue(queue_t *q, int v) { - pthread_mutex_lock(&q->mutex); - while (q->remain == 0) - pthread_cond_wait(&q->not_empty, &q->mutex); - v = q->values[q->rp]; - q->rp++; q->remain--; - if (q->rp == MAX_QUEUE_NUM) q->rp = 0; - pthread_cond_signal(&q->not_full); - pthread_mutex_unlock(&q->mutex); -} - -#endif /* _PPB_QUEUE_H */ diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_data_shared/Makefile --- a/parallel_processing/ppb_data_shared/Makefile Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -TARGET = ppb_data_shared -CC = clang++ -CPPFLAGS = -g -O0 - -$(TARGET): $(TARGET).o - $(CC) -g -O0 -o $@ $< -$(TARGET).o: $(TARGET).cc - -clean: - rm -f $(TARGET) - rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_data_shared/ppb_data_shared.cc --- a/parallel_processing/ppb_data_shared/ppb_data_shared.cc Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -#include -#include - -#define THREAD_NUM 2 -#define DATA_NUM 10 - -typedef struct _thread_arg { - int thread_no; - int *data; - pthread_mutex_t *mutex; -} thread_arg_t; - -void * -thread_func(void *arg) -{ - thread_arg_t *targ = (thread_arg_t *)arg; - int result; - - /* starting mutex and ending mutex*/ - for (int i = 0; i < DATA_NUM; i++) { - pthread_mutex_lock(targ->mutex); - result = targ->data[i] + 1; - sched_yield(); - targ->data[i] = result; - pthread_mutex_unlock(targ->mutex); - } - return 0; -} - -int -main() -{ - pthread_t handle[THREAD_NUM]; - thread_arg_t targ[THREAD_NUM]; - int data[DATA_NUM]; - int i; - pthread_mutex_t mutex; - - /* initialize */ - for (i = 0; i < DATA_NUM; i++) data[i] = 0; - - /* initialized mutex*/ - pthread_mutex_init(&mutex, NULL); - - /* spawn thread a number of THREAD_NUM */ - for (i = 0; i < THREAD_NUM; i++) { - targ[i].thread_no = i; - targ[i].data = data; - targ[i].mutex = &mutex; - - /* spawn thread*/ - pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); - } - - /* wait for running all thread */ - for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); - - /* destroy mutex*/ - pthread_mutex_destroy(&mutex); - - for (i = 0; i < DATA_NUM; i++) printf("data%d : %d\n", i, data[i]); - return 0; -} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_data_split/Makefile --- a/parallel_processing/ppb_data_split/Makefile Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -TARGET = ppb_data_split -CC = clang++ -CPPFLAGS = -g -O0 - -$(TARGET): $(TARGET).o - $(CC) -g -O0 -o $@ $< -$(TARGET).o: $(TARGET).cc - -clean: - rm -f $(TARGET) - rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_data_split/ppb_data_split.cc --- a/parallel_processing/ppb_data_split/ppb_data_split.cc Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -#include -#include - -#define THREAD_NUM 2 -#define DATA_NUM 10 -#define SPLIT_DATA_NUM (DATA_NUM / THREAD_NUM) - -typedef struct _thread_arg { - int thread_no; - int *data; -} thread_arg_t; - -void * -thread_func(void *arg) -{ - thread_arg_t *targ = (thread_arg_t *)arg; - - for (int i = 0; i < SPLIT_DATA_NUM; i++) { - printf("thread%d : %d + 1 = %d\n", - targ->thread_no, targ->data[i], targ->data[i] + 1); - } - return 0; -} - -int -main() -{ - pthread_t handle[THREAD_NUM]; - thread_arg_t targ[THREAD_NUM]; - int data[DATA_NUM]; - int i; - - /* initialize */ - for (i = 0; i < DATA_NUM; i++) data[i] = i; - - /* spawn thread a number of THREAD_NUM */ - for (i = 0; i < THREAD_NUM; i++) { - targ[i].thread_no = i; - - /* divide a data into THREAD_NUM*/ - targ[i].data = &data[SPLIT_DATA_NUM * i]; - /* spawn thread*/ - pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); - } - - /* wait for running all thread */ - for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); - return 0; -} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_first_thread/Makefile --- a/parallel_processing/ppb_first_thread/Makefile Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -TARGET = ppb_first_thread -CC = clang++ -CPPFLAGS = -g -O0 - -$(TARGET): $(TARGET).o - $(CC) -g -O0 -o $@ $< -$(TARGET).o: $(TARGET).cc - -clean: - rm -f $(TARGET) - rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_first_thread/ppb_first_thread.cc --- a/parallel_processing/ppb_first_thread/ppb_first_thread.cc Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -#include -#include - -#define THREAD_NUM 2 -#define DATA_NUM 10 - -typedef struct _thread_arg { - int thread_no; - int *data; -} thread_arg_t; - -void * -thread_func(void *arg) -{ - thread_arg_t *targ = (thread_arg_t *)arg; - - for (int i = 0; i < DATA_NUM; i++) { - printf("thread%d : %d + 1 = %d\n", - targ->thread_no, targ->data[i], targ->data[i] + 1); - } - return 0; -} - -int -main() -{ - pthread_t handle[THREAD_NUM]; - thread_arg_t targ[THREAD_NUM]; - int data[DATA_NUM]; - int i; - - /* initialize */ - for (i = 0; i < DATA_NUM; i++) data[i] = i; - - /* spawn thread a number of THREAD_NUM */ - for (i = 0; i < THREAD_NUM; i++) { - targ[i].thread_no = i; - targ[i].data = data; - - /* spawn thread*/ - pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); - } - - /* wait for running all thread */ - for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); - return 0; -} diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_sem_counter/Makefile --- a/parallel_processing/ppb_sem_counter/Makefile Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -TARGET = ppb_sem_counter -CC = clang++ -CPPFLAGS = -g -O0 - -$(TARGET): $(TARGET).o - $(CC) -g -O0 -o $@ $< -$(TARGET).o: $(TARGET).cc - -clean: - rm -f $(TARGET) - rm -f *.o diff -r d137f1823794 -r 508b47c8f4d8 parallel_processing/ppb_sem_counter/ppb_sem_counter.cc --- a/parallel_processing/ppb_sem_counter/ppb_sem_counter.cc Tue Jan 07 14:15:51 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -#include -#include -#include -#include - -#define MAX_THREAD_NUM 2 -#define THREAD_NUM 5 - -sem_t sem; - -/** - * Mavericks では clang が default となっているが、書籍自体は gcc でコンパイルしている。 - * クラスタ環境に gcc が存在していたので、そこでコンパイルすると望ましい動作結果が得られた。 - * clang では・・・どうするんだろ - */ -void * -thread_func(void *arg) -{ - long id = (long)arg; - - /* starting semaphore and ending semaphore*/ - sem_wait(&sem); - printf("Thread %ld started.\n", id); - sleep(1); - printf("Thread %ld finished.\n", id); - sem_post(&sem); - return 0; -} - -int -main() -{ - pthread_t handle[THREAD_NUM]; - long i; - - /* initialize */ - sem_init(&sem, 0, MAX_THREAD_NUM); - - /* spawn thread a number of THREAD_NUM */ - for (i = 0; i < THREAD_NUM; ++i) pthread_create(&handle[i], NULL, &thread_func, (void*)i); - - /* wait for running all thread */ - for (i = 0; i < THREAD_NUM; ++i) pthread_join(handle[i], NULL); - - /* destroy mutex*/ - sem_destroy(&sem); - - return 0; -}