线程终止问题(C):最后一个线程未终止

线程终止问题(C):最后一个线程未终止,c,multithreading,race-condition,termination,C,Multithreading,Race Condition,Termination,为什么我的最后一个线程没有终止 我想同时启动所有线程,因此我有十个创建好的线程被置于睡眠状态。一旦创建了所有线程,我就会唤醒所有线程,开始处理数据。除最后一个线程#9外,所有线程都已完成。有人知道为什么最后一个线程没有终止并加入所有其他pthread吗 输出: ------------------Theatre Seating---------------- 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 00

为什么我的最后一个线程没有终止

我想同时启动所有线程,因此我有十个创建好的线程被置于睡眠状态。一旦创建了所有线程,我就会唤醒所有线程,开始处理数据。除最后一个线程#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
------------------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
代码:

*创建于2016年7月4日上午11:27
*/
#包括
#包括
pthread_cond_t cond=pthread_cond_初始值设定项;
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
//卖方线程服务一个时间片(1分钟)
#包括
//全局变量
//顾客人数
int N;
char*剧院[][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"}
};
结构类型{
char-st;
国际贸易署;
int numid;
字符*名称;
};
空座位()
{
char*c1=剧院[0][2];
char*d1=剧院[0][0];
//asprintf(&d1,“%c%c%c%d\n”,c1[0],c1[1],卖方类型1->st,卖方类型1->tid);
printf(“D:%s”,d1);
}
作废*出售(作废*arg)
{
结构卖方类型*卖方类型1=arg;
int i;
i=0;
printf(“线程#%d:卖方类型1->st=%c\n”,卖方类型1->tid,
卖方(类型1->st);
//工作
而(i==0){
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
pthread_mutex_unlock(&mutex);
开关(卖方类型1->st){
案例“H”:
printf(“卖方类型:H\n”);
//printf(“卖方名称:%s\n”,卖方类型1->name);
//开始售票
i=1;
//设定时间;
打破
案例“M”:
printf(“卖方类型:M\n”);
printf(“卖方名称:%s\n”,卖方类型1->name);
//开始售票
i=1;
//设定时间;
打破
案例“L”:
printf(“卖方类型:L\n”);
//printf(“卖方名称:%s\n”,卖方类型1->name);
//开始售票
i=1;
//设定时间;
打破
}
}
printf(“线程#%d:工作完成\n”,卖方类型1->tid);
返回NULL;
}
无效剧院()
{
int i,j;
printf(“\n--------------------剧院座位-------------------\n\n”);

对于(i=0;i线程现在可以工作了。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
------------------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
代码:

/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
/* 
*文件:ticketsellers.c
*作者:IantheFlyinghawaian
*
*创建于2016年7月4日上午11:27
*/
#包括
#包括
pthread_cond_t cond=pthread_cond_初始值设定项;
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
//卖方线程服务一个时间片(1分钟)
#包括
//全局变量
//顾客人数
int N;
char*剧院[][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"}
};
结构类型{
char-st;
国际贸易署;
int numid;
字符*名称;
};
空座位()
{
char*c1=剧院[0][2];
char*d1=剧院[0][0];
//asprintf(&d1,“%c%c%c%d\n”,c1[0],c1[1],卖方类型1->st,卖方类型1->tid);
printf(“D:%s”,d1);
}
作废*出售(作废*arg)
{
结构卖方类型*卖方类型1=arg;
int i;
i=0;
printf(“线程#%d:卖方类型1->st=%c\n”,卖方类型1->tid,
卖方(类型1->st);
//工作
而(i==0){
pthread_mutex_lock(&mutex);
//pthread_cond_wait(&cond,&mutex);
pthread_mutex_unlock(&mutex);
开关(卖方类型1->st){
案例“H”:
printf(“卖方类型:H\n”);
printf(“卖方名称:%s\n”,卖方类型1->name);
//开始售票
i=1;
//设定时间;
打破
案例“M”:
printf(“卖方类型:M\n”);
printf(“卖方名称:%s\n”,卖方类型1->name);
//开始售票
i=1;
//设定时间;
打破
案例“L”:
printf(“卖方类型:L\n”);
printf(“卖方名称:%s\n”,卖方类型1->name);
//开始售票
i=1;
//设定时间;
打破
}
}
printf(“线程#%d:工作完成\n”,卖方类型1->tid);
返回NULL;
}
空剧场