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);
}
私は ライン168コメントアウトした場合:wakeup_all_seller_threads()を。 行67:pthread_mutex_lock(&mutex); 行68:pthread_cond_wait(&cond、&mutex); 行69:pthread_mutex_unlock(&mutex); その後、スレッドは正常に動作します。 – Jwoozy
メインスレッドは 'pthread_cond_wait'と呼ばれる最後のスレッドの前に' pthread_cond_broadcast'を呼び出した可能性があります。 – user3386109
ええと...前にasprintf()を使ったことはありません。指定された文字列に対する純粋なメモリ割り当て。今日何かを学んだ。 –