Mercurial > hg > Members > masakoha > testcode
changeset 6:7b0b23f3538d
first pthread programming
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 02 Jan 2014 16:57:28 +0900 |
parents | 4b742b1fedb8 |
children | edf4445da580 |
files | parallel_processing/ppb_first_thread/Makefile parallel_processing/ppb_first_thread/ppb_first_thread.cc |
diffstat | 2 files changed, 51 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/parallel_processing/ppb_first_thread/Makefile Thu Jan 02 15:18:21 2014 +0900 +++ b/parallel_processing/ppb_first_thread/Makefile Thu Jan 02 16:57:28 2014 +0900 @@ -1,5 +1,11 @@ +TARGET = ppb_first_thread CC = clang++ CPPFLAGS = -g -O0 -ppb_first_thread: ppb_first_thread.o + +$(TARGET): $(TARGET).o $(CC) -g -O0 -o $@ $< -ppb_first_thread.o: ppb_first_thread.cc +$(TARGET).o: $(TARGET).cc + +clean: + rm -f $(TARGET) + rm -f *.o
--- a/parallel_processing/ppb_first_thread/ppb_first_thread.cc Thu Jan 02 15:18:21 2014 +0900 +++ b/parallel_processing/ppb_first_thread/ppb_first_thread.cc Thu Jan 02 16:57:28 2014 +0900 @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <pthread.h> + +#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; + + for (i = 0; i < DATA_NUM; i++) data[i] = i; + + for (i = 0; i < THREAD_NUM; i++) { + targ[i].thread_no = i; + targ[i].data = data; + + pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); + } + + for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); + return 0; +}