카테고리 없음

pthread_join vs. pthread_mutex_lock

sunshout 2015. 3. 20. 19:38

일반적으로 thread 를 생성하면 main 은 pthread_join 을 통해서 child thread가 끝날 때까지 기다려야 한다.


하지만 main 에서 pthread_join 대신에 자신을 deadlock 시키는 것이 pthread_join 을 사용하는 것보다 성능상의 이득을 볼 수 있다고 한다. 


#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>


pthread_mutex_t g_NONSTOP;


void *t_func(void *data)

{

    int id;

    id = *((int *)data);

    while(1)

    {

        //printf("(%lu) [%d]\n",pthread_self(), id);

    }

}


int main()

{

    pthread_t tid;

    int a = 1;

    int thr_id;

    thr_id = pthread_create(&tid, NULL, t_func, (void*)&a);

    if ( 0 > thr_id )

    {

        perror("thread create error");

        exit(0);

    }

    pthread_mutex_init(&g_NONSTOP, NULL);

    pthread_mutex_lock(&g_NONSTOP);

    pthread_mutex_lock(&g_NONSTOP);

    return 0;

}




#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>


pthread_mutex_t g_NONSTOP;


void *t_func(void *data)

{

    int id;

    id = *((int *)data);

    while(1)

    {

        //printf("(%lu) [%d]\n",pthread_self(), id);

    }

}


int main()

{

    pthread_t tid;

    int a = 1;

    int thr_id;

    int status;

    thr_id = pthread_create(&tid, NULL, t_func, (void*)&a);

    if ( 0 > thr_id )

    {

        perror("thread create error");

        exit(0);

    }

    pthread_join(tid, (void *)&status);

    return 0;

}