Study/Programming
워커(Worker) 쓰레드 모델
까보자
2014. 5. 28. 10:24
워커(Worker) 쓰레드 모델
두 개의 쓰레드를 생성해서 따로 덧셈을 실행하여 main함수에서는 오로지 연산결과를 출력하는 형태로 작성
이러한 일꾼(Worker)의 형태를 띠는 모델을 워커 쓰레드(Worker thread)모델이라함
#include <stdio.h> #include <pthread.h> void * thread_summation(void * arg); int sum=0; int main(int argc, char *argv[]) { pthread_t id_t1, id_t2; int range1[]={1, 50}; int range2[]={51, 100}; pthread_create(&id_t1, NULL, thread_summation, (void *) range1); pthread_create(&id_t2, NULL, thread_summation, (void *) range2); pthread_join(id_t1, NULL); pthread_join(id_t2, NULL); printf("result: %d \n", sum); return 0; } void * thread_summation(void * arg) { int start=((int*)arg)[0]; int end=((int*)arg)[1]; while(start<=end) { sum+=start; start++; } return NULL; }
|
두 쓰레드가 하나의 전역변수 sum에 직접 접근
결과
1~10까지의 합과 1~100까지의 합을 출력해본 화면