2016-07-05 144 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(&mutex); 第68行:pthread_cond_wait(&cond,&mutex); 第69行:pthread_mutex_unlock(&mutex); 然後線程運行良好。 – 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); 
}