'2014/04/04 글 목록 :: Horizontal Grays S2

지금 하고 있는 프로그램은 크게 3가지 일을 동시에 해야한다. (엄밀히 말해서는 동시에 하는 건 아니고 ㅋ)


1. Camera로 부터 영상획득 - 처리 

2. Bluetooth를 통하여 들어오는 명령 프로토콜 분석

3. Socket을 통하여 영상 전송


이를 동시적으로 처리를 해아하는데 

늘 하던 펌웨어라면 Timer Interrupt 이용해서 이놈했다 저놈했다 하는거 별 어려운일 도 아니겠지만 이쪽 프로그래밍을 하던 사람은 아니라서 검색 ㅋ

당연히 같은 개념인 Thread라는게 존재했다.


이놈을 어떻게 쓰는지 자세한 설명은 아래 링크 참조

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

http://knight76.tistory.com/entry/20010117546


여러 예제와 다양한 기능들이 있다만 내가 하고자 하는 일엔 그다지 복잡한게 필요치 않다.


위의 1,2,3의 일을 동시적으로 처리하기만 하면 된다.


아래는 작성한 예제소스


//============================================================================

// Name        : MultiThreadTest.cpp

// Author      : 

// Version     :

// Copyright   : Your copyright notice

// Description : Hello World in C++, Ansi-style

//============================================================================


#include <iostream>

#include <stdio.h>

#include <sys/time.h>

#include <pthread.h>



using namespace std;



int ncount = 0;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


void* thread1(void* arg);

void* thread2(void* arg);



int main() {

pthread_t thread_id;

pthread_t p_thread1,p_thread2;

int status;


int i = 1;



printf("init main\n");


if((thread_id=pthread_create(&p_thread1,NULL,thread1,NULL)))

{

printf("Thread1[%d] Create Failed!\n",thread_id);

}else printf("Thread1[%d] Create Success!\n",thread_id);



sleep(1);


if((thread_id=pthread_create(&p_thread2,NULL,thread2,NULL)))

{

printf("Thread2[%d] Create Failed!\n",thread_id);

}else printf("Thread2[%d] Create Success!\n",thread_id);


sleep(1);





while(1)

{

pthread_mutex_lock(&mutex);

// thread_id = pthread_self();

printf("main = %d\n",i);

pthread_mutex_unlock(&mutex);

i++;

sleep(1);


if(i>2) break;


}


printf("thread1 cancel\n");


if(pthread_cancel(p_thread1)) printf("pthread_cancel 1 failed\n");

usleep(100);


printf("thread2 cancel\n");


if(pthread_cancel(p_thread2)) printf("pthread_cancel 2 failed\n");

usleep(100);


status = pthread_mutex_destroy(&mutex);

printf("status %d\n",status);

printf("End!!!\n");


return 0;

}


void* thread1(void* arg)

{


int i,j;

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);

pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);


printf("init thread1");

i = 1;

j = 2;

printf("i = %d, j = %d\n",i,j);


while(1)

{

pthread_mutex_lock(&mutex);

printf("thread1 \n");

pthread_mutex_unlock(&mutex);

pthread_testcancel();

usleep(100000);

}

}


void* thread2(void* arg)

{


int i,j;

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);

pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);


printf("init thread2");

i = 3;

j = 4;

printf("i = %d, j = %d\n",i,j);


while(1)

{

pthread_mutex_lock(&mutex);

printf("thread2 \n");

pthread_mutex_unlock(&mutex);

pthread_testcancel();

usleep(100000);

}

}



예제는 별것 없다.


main함수가 시작되면

Thread1과 Thread2를 만듦과 동시에 Thread1, Thread2 구동


main에서는  1초에 한번씩 i 변수 증가하고 출력

thread1은 100ms 마다 한번씩, thread2도 100ms마다 한번씩 각각 난 thread1이요, 난 thread2요 하고 출력


main이 2초가 넘어가면 각 thread cancel 시키고 종료한다.


결과는 아래와 같다.


init main

Thread1[0] Create Success!

init thread1i = 1, j = 2

thread1 

thread1 

thread1 

thread1 

thread1 

thread1 

thread1 

thread1 

thread1 

thread1 

Thread2[0] Create Success!

init thread2i = 3, j = 4

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

main = 1

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

main = 2

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread2 

thread1 

thread1 cancel

thread2 cancel

status 0

End!!!


잘 된다! ^^


여기서 주의해야 할 사항!!


처음에 #include <pthread.h>를 정확히 하였슴에도

pthread관련 함수들이 모두 컴파일 에러

예를 들어 다음과 같은 에러였다.


undefined reference to 'prhread_create'와 같은..


이를 해결하려면 컴파일 옵션을 손봐야 하는데

gcc 사용자라면 -lpthread 옵션을 추가해주면 되고


나처럼 Eclipse에서 개발환경이라면


properties -> C/C++ Build -> Settings  -> C++ Linker -> Libraries 에 pthread를 추가해주면 된다.




+ Recent posts