2016-07-05 6 views
1

왜 마지막 스레드가 종료되지 않습니까?스레드 종단 문제 (C) : 마지막 스레드가 종료되지 않습니다.

동시에 모든 스레드를 시작하고 싶습니다. 그래서 한 번 생성 된 스레드가 10 개나 있습니다. 일단 모든 쓰레드가 생성되면, 나는 그것들 모두를 깨우고 데이터 처리를 시작한다. 스레드는 마지막 스레드, 스레드 # 9를 제외하고 완료됩니다. 마지막 스레드가 종료되지 않고 다른 모든 pthread에 참여하는 이유를 아는 사람이 있습니까?

출력 :

------------------Theatre Seating---------------- 

0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 
0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 
0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 
0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 
0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 
0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 
0060 0061 0062 0063 0064 0065 0066 0067 0068 0069 
0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 
0080 0081 0082 0083 0084 0085 0086 0087 0088 0089 
0090 0091 0092 0093 0094 0095 0096 0097 0098 0099 

------------------End of Seating---------------- 

Enter N value of Customers: 2 
Number of Customers: 2 
thread #0: seller_type1->st = H 
thread #1: seller_type1->st = M 
thread #2: seller_type1->st = M 
thread #3: seller_type1->st = M 
thread #4: seller_type1->st = L 
thread #5: seller_type1->st = L 
thread #6: seller_type1->st = L 
thread #7: seller_type1->st = L 
thread #8: seller_type1->st = L 
thread #9: seller_type1->st = L 
Seller type: H 
thread #0: Work done 
Seller type: M 
Seller type: M 
Seller name: M1 

Seller type: M 
Seller name: M2 

Seller type: L 
Seller type: L 
thread #1: Work done 
Seller type: L 
Seller name: M3 

Seller type: L 
Seller type: L 
thread #2: Work done 
thread #4: Work done 
thread #5: Work done 
thread #6: Work done 
thread #3: Work done 
thread #7: Work done 
thread #8: Work done 

코드 :

* Created on July 4, 2016, 11:27 AM 
*/ 

#include <stdio.h> 
#include <pthread.h> 
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
// seller thread to serve one time slice (1 minute) 

#include <stdlib.h> 

//Global variables 
//Number of Customers 
int N; 
char * theatre[][10] = { 
     {"0000", "0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009"}, 
     {"0010", "0011", "0012", "0013", "0014", "0015", "0016", "0017", "0018", "0019"}, 
     {"0020", "0021", "0022", "0023", "0024", "0025", "0026", "0027", "0028", "0029"}, 
     {"0030", "0031", "0032", "0033", "0034", "0035", "0036", "0037", "0038", "0039"}, 
     {"0040", "0041", "0042", "0043", "0044", "0045", "0046", "0047", "0048", "0049"}, 
     {"0050", "0051", "0052", "0053", "0054", "0055", "0056", "0057", "0058", "0059"}, 
     {"0060", "0061", "0062", "0063", "0064", "0065", "0066", "0067", "0068", "0069"}, 
     {"0070", "0071", "0072", "0073", "0074", "0075", "0076", "0077", "0078", "0079"}, 
     {"0080", "0081", "0082", "0083", "0084", "0085", "0086", "0087", "0088", "0089"}, 
     {"0090", "0091", "0092", "0093", "0094", "0095", "0096", "0097", "0098", "0099"} 
}; 


struct seller_type { 
    char st; 
    int tid; 
    int numid; 
    char * name; 
}; 


void fillSeat() 
{ 
    char *c1 = theatre[0][2]; 
    char *d1 = theatre[0][0]; 

     //asprintf(&d1, "%c%c%c%d\n",c1[0],c1[1],seller_type1->st,seller_type1->tid); 
     printf("D : %s", d1); 
} 

void *sell(void *arg) 
{ 
    struct seller_type *seller_type1 = arg; 
    int i; 
    i = 0; 
    printf("thread #%d: seller_type1->st = %c\n", seller_type1->tid, 
    seller_type1->st); 


    // do work 
    while (i == 0) { 
    pthread_mutex_lock(&mutex); 
    pthread_cond_wait(&cond, &mutex); 
    pthread_mutex_unlock(&mutex); 

    switch (seller_type1->st) { 

    case 'H': 
     printf("Seller type: H\n"); 
     //printf("Seller name: %s\n", seller_type1->name); 

     //Begin Selling tickets 
     i = 1; 
     //set time; 

     break; 

    case 'M': 
     printf("Seller type: M\n"); 
     printf("Seller name: %s\n", seller_type1->name); 

     //Begin Selling tickets 
     i = 1; 
     //set time; 
     break; 

    case 'L': 
     printf("Seller type: L\n"); 
     //printf("Seller name: %s\n", seller_type1->name); 

     //Begin Selling tickets 
     i = 1; 
     //set time; 
     break; 

    } 


    } 
    printf("thread #%d: Work done\n", seller_type1->tid); 
    return NULL; 
} 

void displayTheatre() 
{ 
    int i,j; 
    printf("\n------------------Theatre Seating----------------\n\n"); 
    for(i = 0;i<10;i++) 
    { 
     for(j = 0;j<10;j++){ 
      printf("%s ",theatre[i][j]); 
     } 
     printf("\n"); 
    } 
    printf("\n------------------End of Seating----------------\n\n"); 
} 

void wakeup_all_seller_threads() 
{ 
pthread_mutex_lock(&mutex); 
pthread_cond_broadcast(&cond); 
pthread_mutex_unlock(&mutex); 

} 

int main() 
{ 
    N = 0; 
    int i = 0; 
    struct seller_type *seller_type1; 
    displayTheatre(); 
    pthread_t tids[10]; 
    seller_type1 = calloc(10, sizeof(struct seller_type)); 

    printf("Enter N value of Customers: "); 
    scanf("%d", &N); 
    printf("Number of Customers: %d\n", N); 

    // All error handling ommitted! Yes, ALL! 

    seller_type1[0].st = 'H'; 
    seller_type1[0].tid = 0; 
    seller_type1[0].numid = 0; 
    asprintf(&seller_type1[0].name, "%c%d\n",seller_type1[0].st,seller_type1[0].numid); 
    pthread_create(&tids[0], NULL, sell, &seller_type1[0]); 

    for (i = 1; i < 4; i++) { 
    seller_type1[i].st = 'M'; 
    seller_type1[i].tid = i; 
    seller_type1[i].numid = i; 
    asprintf(&seller_type1[i].name, "%c%d\n",seller_type1[i].st,seller_type1[i].numid); 
    pthread_create(&tids[i], NULL, sell, &seller_type1[i]); 
    } 

    for (i = 4; i < 10; i++) { 
    seller_type1[i].st = 'L'; 
    seller_type1[i].tid = i; 
    seller_type1[i].numid = i-3; 
    asprintf(&seller_type1[i].name, "%c%d\n",seller_type1[i].st,seller_type1[i].numid); 
    pthread_create(&tids[i], NULL, sell, &seller_type1[i]); 
    } 

    wakeup_all_seller_threads(); 
    //puts("All threads Start"); 

    // wait for all seller threads to exit 
    for (i = 0; i < 10; i++) { 
    pthread_join(tids[i], NULL); 
    //printf("Thread %d joined\n", i); 
    } 
    //puts("All threads joined"); 

    exit(EXIT_SUCCESS); 
} 
+0

내가 라인 (168) 주석 경우 :) (wakeup_all_seller_threads을; 라인 67 : pthread_mutex_lock (& ​​뮤텍스); 68 행 : pthread_cond_wait (& cond, & mutex); 라인 69 : pthread_mutex_unlock (뮤텍스); 그러면 스레드가 정상적으로 실행됩니다. – Jwoozy

+3

주 스레드가 마지막 스레드 인'pthread_cond_wait' 전에'pthread_cond_broadcast'라고 부르는 것 같습니다. – user3386109

+0

어 ... 전에 asprintf()를 사용하지 않았어요. 깔끔한 - 주어진 문자열에 대한 메모리 할당. 오늘 뭔가 배웠습니다. –

답변

0

스레딩이 작동합니다. pthread_cond가 제거되고 주 스레드가 뮤텍스를 잠급니다. 쓰레드가 만들어졌고 다른 것들을하기 전에 뮤텍스를 잠그고 잠그지 않았습니다. 그런 다음 메인은 뮤텍스를 잠금 해제하여 동시에 모두 시작합니다.

출력 :

------------------Theatre Seating---------------- 

0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 
0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 
0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 
0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 
0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 
0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 
0060 0061 0062 0063 0064 0065 0066 0067 0068 0069 
0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 
0080 0081 0082 0083 0084 0085 0086 0087 0088 0089 
0090 0091 0092 0093 0094 0095 0096 0097 0098 0099 

------------------End of Seating---------------- 

Enter N value of Customers: 3 
Number of Customers: 3 
thread #0: seller_type1->st = H 
thread #1: seller_type1->st = M 
thread #2: seller_type1->st = M 
thread #3: seller_type1->st = M 
thread #4: seller_type1->st = L 
thread #5: seller_type1->st = L 
thread #6: seller_type1->st = L 
thread #7: seller_type1->st = L 
thread #8: seller_type1->st = L 
thread #9: seller_type1->st = L 
Seller type: H 
Seller type: M 
Seller type: M 
Seller type: M 
Seller name: H0 

Seller type: L 
Seller name: M1 

Seller type: L 
Seller type: L 
Seller name: M2 

Seller type: L 
Seller name: M3 

Seller type: L 
Seller type: L 
thread #0: Work done 
Seller name: L1 

thread #1: Work done 
Seller name: L2 

Seller name: L3 

thread #2: Work done 
Seller name: L4 

thread #3: Work done 
Seller name: L5 

Seller name: L6 

thread #4: Work done 
thread #5: Work done 
thread #6: Work done 
thread #7: Work done 
thread #8: Work done 
thread #9: Work done 

RUN FINISHED; exit value 0; real time: 1s; user: 0ms; system: 0ms 

코드 :

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* File: ticketsellers.c 
* Author: iantheflyinghawaiian 
* 
* Created on July 4, 2016, 11:27 AM 
*/ 

#include <stdio.h> 
#include <pthread.h> 
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
// seller thread to serve one time slice (1 minute) 

#include <stdlib.h> 

//Global variables 
//Number of Customers 
int N; 
char * theatre[][10] = { 
     {"0000", "0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009"}, 
     {"0010", "0011", "0012", "0013", "0014", "0015", "0016", "0017", "0018", "0019"}, 
     {"0020", "0021", "0022", "0023", "0024", "0025", "0026", "0027", "0028", "0029"}, 
     {"0030", "0031", "0032", "0033", "0034", "0035", "0036", "0037", "0038", "0039"}, 
     {"0040", "0041", "0042", "0043", "0044", "0045", "0046", "0047", "0048", "0049"}, 
     {"0050", "0051", "0052", "0053", "0054", "0055", "0056", "0057", "0058", "0059"}, 
     {"0060", "0061", "0062", "0063", "0064", "0065", "0066", "0067", "0068", "0069"}, 
     {"0070", "0071", "0072", "0073", "0074", "0075", "0076", "0077", "0078", "0079"}, 
     {"0080", "0081", "0082", "0083", "0084", "0085", "0086", "0087", "0088", "0089"}, 
     {"0090", "0091", "0092", "0093", "0094", "0095", "0096", "0097", "0098", "0099"} 
}; 


struct seller_type { 
    char st; 
    int tid; 
    int numid; 
    char * name; 
}; 


void fillSeat() 
{ 
    char *c1 = theatre[0][2]; 
    char *d1 = theatre[0][0]; 

     //asprintf(&d1, "%c%c%c%d\n",c1[0],c1[1],seller_type1->st,seller_type1->tid); 
     printf("D : %s", d1); 
} 

void *sell(void *arg) 
{ 
    struct seller_type *seller_type1 = arg; 
    int i; 
    i = 0; 
    printf("thread #%d: seller_type1->st = %c\n", seller_type1->tid, 
    seller_type1->st); 


    // do work 
    while (i == 0) { 
    pthread_mutex_lock(&mutex); 
    //pthread_cond_wait(&cond, &mutex); 
    pthread_mutex_unlock(&mutex); 

    switch (seller_type1->st) { 

    case 'H': 
     printf("Seller type: H\n"); 
     printf("Seller name: %s\n", seller_type1->name); 

     //Begin Selling tickets 
     i = 1; 
     //set time; 

     break; 

    case 'M': 
     printf("Seller type: M\n"); 
     printf("Seller name: %s\n", seller_type1->name); 

     //Begin Selling tickets 
     i = 1; 
     //set time; 
     break; 

    case 'L': 
     printf("Seller type: L\n"); 
     printf("Seller name: %s\n", seller_type1->name); 

     //Begin Selling tickets 
     i = 1; 
     //set time; 
     break; 

    } 


    } 
    printf("thread #%d: Work done\n", seller_type1->tid); 
    return NULL; 
} 

void displayTheatre() 
{ 
    int i,j; 
    printf("\n------------------Theatre Seating----------------\n\n"); 
    for(i = 0;i<10;i++) 
    { 
     for(j = 0;j<10;j++){ 
      printf("%s ",theatre[i][j]); 
     } 
     printf("\n"); 
    } 
    printf("\n------------------End of Seating----------------\n\n"); 
} 

void wakeup_all_seller_threads() 
{ 
//pthread_mutex_lock(&mutex); 
//pthread_cond_wait(&cond, &mutex); 
//pthread_cond_broadcast(&cond); 
pthread_mutex_unlock(&mutex); 

} 

void lockup_all_seller_threads() 
{ 
pthread_mutex_lock(&mutex); 
//pthread_cond_wait(&cond, &mutex); 
//pthread_cond_broadcast(&cond); 
//pthread_mutex_unlock(&mutex); 

} 

int main() 
{ 
    lockup_all_seller_threads(); 

    N = 0; 
    int i = 0; 
    struct seller_type *seller_type1; 
    displayTheatre(); 
    pthread_t tids[10]; 
    seller_type1 = calloc(10, sizeof(struct seller_type)); 

    printf("Enter N value of Customers: "); 
    scanf("%d", &N); 
    printf("Number of Customers: %d\n", N); 

    // All error handling ommitted! Yes, ALL! 

    seller_type1[0].st = 'H'; 
    seller_type1[0].tid = 0; 
    seller_type1[0].numid = 0; 
    asprintf(&seller_type1[0].name, "%c%d\n",seller_type1[0].st,seller_type1[0].numid); 
    pthread_create(&tids[0], NULL, sell, &seller_type1[0]); 

    for (i = 1; i < 4; i++) { 
    seller_type1[i].st = 'M'; 
    seller_type1[i].tid = i; 
    seller_type1[i].numid = i; 
    asprintf(&seller_type1[i].name, "%c%d\n",seller_type1[i].st,seller_type1[i].numid); 
    pthread_create(&tids[i], NULL, sell, &seller_type1[i]); 
    } 

    for (i = 4; i < 10; i++) { 
    seller_type1[i].st = 'L'; 
    seller_type1[i].tid = i; 
    seller_type1[i].numid = i-3; 
    asprintf(&seller_type1[i].name, "%c%d\n",seller_type1[i].st,seller_type1[i].numid); 
    pthread_create(&tids[i], NULL, sell, &seller_type1[i]); 
    } 

    wakeup_all_seller_threads(); 
    //puts("All threads Start"); 

    // wait for all seller threads to exit 
    for (i = 0; i < 10; i++) { 
    pthread_join(tids[i], NULL); 
    //printf("Thread %d joined\n", i); 
    } 
    //puts("All threads joined"); 

    exit(EXIT_SUCCESS); 
} 
관련 문제