Mercurial > hg > Members > masakoha > testcode
view parallel_processing/ppb_first_thread/ppb_first_thread.cc @ 7:edf4445da580
add comment
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 03 Jan 2014 01:36:18 +0900 |
parents | 7b0b23f3538d |
children |
line wrap: on
line source
#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; /* 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; }