'pthread'에 해당되는 글 1건

  1. 2015.04.14 pthread

pthread

Linux/Linux:c개발 2015. 4. 14. 11:28

참조: http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Thread/Beginning/PthreadApiReference 



Thread의 상태:

PTHREAD_CREATE_JOINABLE  : 다른 thread끝나고 동작하는 thread. (exit나 join을 호출을 통해서 자원을 반납해야 하는 thread.)

PTHREAD_CREATE_DETACHED : 독립적으로 동작하므로 종료시점 체크필요없고, 종료시 자동자원반납. 



<구조체>


typedef  long pthread_t;

struct pthread_attr {
  void *stackaddr;
  size_t stacksize;
  int detachstate;
  struct sched_param param;
  int inheritsched;
  int contentionscope;
};

typedef struct pthread_attr pthread_attr_t;
#define PTHREAD_CREATE_JOINABLE       0
#define PTHREAD_CREATE_DETACHED       1


<예제 - 심플 detatched thread 생성. >

 pthread_t id;

 pthread_attr_t attributes;  



// initialize thread attributes

    pthread_attr_init(&attributes)

    pthread_attr_setdetachstate( &attributes, PTHREAD_CREATE_DETACHED);


//create a new thread 

   pthread_create( &myId, &attributes, startFunc, args);  //args는 startFunc로 전달. 

   pthread_attr_destroy(&attributes); //자원해제 



Posted by yongary
,