【发布时间】:2016-07-05 20:39:48
【问题描述】:
为什么我的最后一个线程没有终止?
我想同时启动所有线程,所以我有十个线程一旦创建就进入睡眠状态。创建所有线程后,我唤醒所有线程以开始处理数据。除了最后一个线程 #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);
}
【问题讨论】:
-
如果我注释掉第 168 行:wakeup_all_seller_threads();第 67 行:pthread_mutex_lock(&mutex);第 68 行:pthread_cond_wait(&cond, &mutex);第 69 行:pthread_mutex_unlock(&mutex);然后线程运行良好。
-
很可能主线程调用
pthread_cond_broadcast在最后一个线程调用pthread_cond_wait之前。 -
嗯...以前从未使用过 asprintf()。 Neat - 给定字符串的内存分配。今天学到了一些东西。
-
@Jwoozy 我会完全摆脱
pthread_cond。让main锁定互斥锁。然后创建线程,并解锁互斥锁。在执行任何其他操作之前,让每个线程锁定并解锁互斥锁。这样线程必须等到main解锁互斥锁才能启动。 -
谢谢 user3386109,它现在可以工作了。我会为其他人重新发布工作代码。
标签: c multithreading race-condition termination