2012-07-12 4 views
3

멀티 스레드 프로그래밍에서 '잠금'을 사용해야하는시기는 언제입니까? 각 스레드가 수정하거나 수정하지 않아도 각 스레드가 액세스 할 수있는 영역을 잠글 영역을 잠그십시오. 좀 더 시간이 있다면 귀하의 관심멀티 스레드 프로그래밍에서 '잠금'을 사용해야하는시기는 언제입니까?

struct share_data { 
    /* share data */ 
    thread_id; 
} 

thread 1 will exceute main() function: 
    Initial share data. /* need lock */ 

    join all thread(share_data.thread_id, &status) /* access share data thread_id, lock or not? */ 
    exit. 

other threads will: 
    access share_data, /* lock or not? */ 
    modify share_data, /* lock */ 
    exit. 

덕분에, 그리고 실제 코드에 대한 자세한 내용 :

/* 
the number of threads will be input by user. Structure "tdata" and "tlist" stores 
information of each thread, including: "tid" - thread id which is gaven by 1st argument 
of pthread_create(), "torder" which is the order of calling pthread_create() for each 
thead, "status" stores work status of each thread. I allocate memory for "tlist" 
dynamically. Now, we assume that the number of threads is NUM_THREADS(5). I do not use 
the 4th argument to pass data to each thread, I use global variable "tdata", "tlist" as 
shared data to them. "mutex" variable is used to make sure those threads share data safely, 
but it seems not works correctly. 

I wanna each thread can get "torder", and maybe modify the status before calling 
pthread_exit(). 
*/ 
#include <pthread.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 

/* #define NUM_THREADS 5 */ 
struct tdata { 
     pthread_t tid; 
     int torder; 
     int status; 
     struct tdata *next; 
}; 
typedef struct tdata tdata_t; 
struct tdatalist { 
     tdata_t *head; 
     tdata_t *tail; 
}tlist; 

pthread_mutex_t mutex; 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
void *tfunc() { 
     tdata_t *p, *q; 
     unsigned long int tid; 
     int torder; 

     p = tlist.head; 
     q = NULL; 
     pthread_mutex_lock(&mutex); 

     tid = pthread_self(); 

     while (p->tid!=tid && p->next!=NULL) { 
       q = p; 
       p = p->next; 
     } 
     tid = p->tid; 
     torder = p->torder; 
     /* p->status = 0; */ 
     pthread_mutex_unlock(&mutex); 
     /* printf ("I am thread %lu, myorder %d, thread_exit.\n", tid, torder); */ 
     printf ("I am thread %0x, myorder %d, thread_exit.\n", tid, torder); 

     pthread_exit((void *)torder); 
} 

int main (int argc, char *argv[]) { 
/*  pthread_t thread[NUM_THREADS]; */ 

     pthread_attr_t attr; 
     int t; 
     tdata_t *tdata_p; 
     int num_threads; 
     printf ("Input number of threads:"); 
     scanf ("%d", &num_threads); 
     printf ("\n"); 

     printf ("Main thread id: %08x\n", pthread_self()); 

     pthread_mutex_init(&mutex, NULL); 
     pthread_attr_init(&attr); 
     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 

     tlist.head=tlist.tail=NULL;  /* create and initial tlist */ 
     for (t=0; t<num_threads; t++) { 

       pthread_mutex_lock(&mutex); 
       tdata_p = (tdata_t *) malloc (sizeof (tdata_t)); 
       pthread_create (&tdata_p->tid, &attr, tfunc, NULL); 
       /* tdata_p->tid = thread[t]; */ 
       tdata_p->torder = t; 
       tdata_p->status = 1;   /* for 0,finished the work. for 1,not*/ 
       tdata_p->next = NULL; 

       if(tlist.head == NULL) { 
         tlist.head = tlist.tail = tdata_p; 
       } 
       else { 
         tlist.tail->next = tdata_p; 
         tlist.tail = tdata_p; 
       } 
       pthread_mutex_unlock(&mutex); 

     } 

     /* Join child threads */ 
     pthread_attr_destroy(&attr); 
     pthread_mutex_lock (&mutex); 
     tdata_t *p; 
     tdata_t *q; 
     void *status; 
     p = tlist.head; 
     while (p != NULL) { 
       q = p->next; 
       pthread_join(p->tid, &status); 
       p = q; 
     } 

     pthread_mutex_unlock (&mutex); 

     pthread_mutex_destroy(&mutex); 
     /* delete the list */ 
     p = tlist.head; 
     while (p != NULL) { 
       q = p->next; 
       free (p); 
       p = q; 
     } 
     tlist.head = tlist.tail = NULL; 

     printf ("Main exit.\n"); 
     pthread_exit(NULL); 

     return 0; 
} 
+0

공유 데이터에 대한 * 모든 * 읽기/쓰기 액세스를 잠글 필요가 있습니다. –

+0

일반적으로 변경 가능한 데이터에 액세스해야 할 때마다 잠금을 설정하고 액세스하거나 수정하는 동안 다른 스레드가 데이터에 액세스하거나 수정하지 못하도록하고 싶습니다. 실제로 코드의 중요한 영역을 잠그면 동일한 위험 영역에 들어가는 (즉, 동일한 데이터에 액세스하는) 다른 스레드는 위험 영역에 들어가거나 동일한 데이터에 액세스하기 전에 잠금을 해제 할 때까지 대기해야합니다. 즉, 액세스 share_data : 잠금; 수정 share_data : 잠금; https://www.google.com/#q=posix+lock+tutorial –

답변

3

읽거나 데이터를 기록하려고 할 때마다, 당신은 그것을 고정해야합니다. 이렇게하면 데이터가 아직 쓰여지지 않은 데이터를 읽지 못하게됩니다.

또 다른 방법은 스레드 또는 프로세스간에 공유되는 모든 데이터를 변경하거나 읽기 전에 잠궈 야합니다.

+0

하지만 pthread를 사용하여 스레드를 결합하면 pthread_join()의 첫 번째 인수가 공유 데이터 구조체에 저장됩니다. pthread_join()을 잠그면 주 스레드에서 호출 된 pthread_join()이 대기 스레드 종료를 위해 일시 ​​중단되고 스레드는 주 스레드 호스트가 대기하는 잠금 상태가 될 것이기 때문에 데드락이 발생합니다. 어떻게해야합니까? –

+0

pthread_join을 호출 할 때 교착 상태에 대해 걱정할 필요가 없습니다. pthread_join은 종료 스레드에 의해서만 호출되어야합니다. 즉, pthread_close가 호출 될 때 다른 스레드가 의존하는 main의 모든 데이터가 기록되고 잠금이 제거되어야합니다. 만약 당신이 변수를 기다리는 것에 참여하고있는 스레드를 제공했다면, fron pthread join이 저장되어있는 결과가 교착 상태에 빠지게됩니다. 다른 스레드가 해당 값을 검사하여 스레드를 기다리고 있는지 확인해야하는 경우 조건문을 사용해야합니다. – retrohacker

+0

데이터에 쓸 때 공유 데이터에 대한 잠금을 사용해야하는 이유를 이해하지만 읽기 위해 잠글 필요가있는 이유를 이해하지 못합니다. 읽기 중에 두 스레드가 동시에 동일한 데이터에 액세스하는 경우 데이터를 변경하지 않습니다. 데이터의 변경이 일어나지 않기 때문에이 경우 잠금이 필요하지 않습니까? –

0

상위 수준의 스레드 간 통신 (예 : 생산자 - 소비자 대기열)의 단기 잠금 이외에는 "자주 벗어날 수있는만큼 드물게"라고 대답 할 것입니다. 잠금은 교착 상태를 발생시키고 교착 상태의 가능성은 수퍼 선형 방식으로 더 많은 잠금과 곱합니다.

또한 앱에서 좋은 pthread_join() 호출은 0입니다. < 0은 불가능하고 1 이상은 너무 많이 있습니다.

+0

pthread_join()을 호출하지 않고 주 스레드에서 다른 스레드를 대기시키는 방법? –

+2

잠금은 교착 상태를 생성하지 않으며 잠금을 부적절하게 사용하고 경쟁 조건으로 인해 교착 상태가 발생합니다. 잠금을 사용하지 않으면 세그멘테이션 오류가 발생하고 디버깅이 악몽이 될 수 있습니다. 둘 이상의 스레드가 액세스 할 메모리를 공유 할 때마다 잠글 필요가 있습니다. 나중에 코드를 추가 할 때 모든 공유 메모리를 잠글 수있는 좋은 방법을 코드 유지 관리를 원할 경우 이미 경쟁 조건으로부터 보호되며 통과에 대해 걱정할 필요가 없습니다 오래된 코드는 물건을 잠그고 잠금 해제해야하는 곳을 찾습니다. – retrohacker

+0

요점은'모든 스레드에 합류 (share_data.thread_id, & status)/* 액세스 공유 데이터 thread_id, 잠금 또는 안돼? * /'share_data.thread_id "에 접근 할 때, 나는이 같은 코드를 잠글 수 없다.'pthread_mutex_lock (& ​​mutex); pthread_join (share_data.thread_id, & status); pthread_mutex_unlock (뮤텍스)' –

관련 문제