워커(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까지의 합을 출력해본 화면
'Study > Programming' 카테고리의 다른 글
쓰레드 동기화 뮤텍스(Mutex) (0) | 2014.06.02 |
---|---|
안드로이드 액티비티와 인텐트 (0) | 2014.05.30 |
MySQL 설치 및 확인 (0) | 2014.05.19 |
안드로이드 스레드 AsyncTask 사용 (0) | 2014.05.17 |
안드로이드 프로세스와 스레드 (스레드) (0) | 2014.05.06 |