<分区>
我正在创建两个不同的 pthread
通过 pthread_create()
并通过调用 pthread_join()
等待他们退出.
当我运行程序时,最后的输出是“等待线程 1 完成...”,因此它实际上在尝试加入第一个线程时停止,然后终止。
当我只创建和运行一个线程并等待其执行完成时使用 pthread_join()
,一切正常,输出符合预期。
我在这里错过了什么?我已经在网上搜索并发现 pthread_join()
创建多个线程时表现不正常。但是我实际上如何使用它来等待完成两个 pthread
的执行?是吗?
这是我的代码:
typedef struct
{
double speed_in_kmh;
double current_power_in_mega_watts;
} DeLorean;
typedef struct
{
unsigned int* cap_indices;
unsigned int array_length;
} IndexContainer;
typedef struct {
unsigned char* c_string;
unsigned int value;
} FluxCapacitor;
DeLorean* delorean__;
FluxCapacitor** capacitors__;
pthread_mutex_t lock;
// Thread function.
void* assembleDeLorean(void* indices)
{
// Get mutex to lock function and make sure
// that only one thread at a time is using it.
pthread_mutex_lock(&lock);
// Declare "indices" parameter as IndexContainer.
IndexContainer* iC = indices;
double sum = 0;
// Iterate through all given indices in iC
// and add value of iC to sum, if index exists.
for (int i = 0; i < 121; ++i)
{
for (int j = 0; j < iC->array_length; ++j)
{
if (i == iC->cap_indices[j])
{
sum += capacitors__[i]->value;
break;
}
}
}
// Assign computed sum to power of global delorean.
delorean__->current_power_in_mega_watts = sum;
// Release mutex.
pthread_mutex_unlock(&lock);
// Stop thread.
pthread_exit(NULL);
}
int main(void)
{
printf("Main thread \"main()\" was started.\n");
capacitors__ = createFluxCapacitorArray(121);
delorean__ = createDeLorean(0, 0);
IndexContainer* iC_1 = malloc(sizeof(*iC_1));
iC_1->array_length = 21;
iC_1->cap_indices = malloc(21 * sizeof(unsigned int));
IndexContainer* iC_2 = malloc(sizeof(*iC_2));
iC_2->array_length = 100;
iC_2->cap_indices = malloc(100 * sizeof(unsigned int));
// Fill iC_1.
for (int i = 0; i < 21; ++i)
{
iC_1->cap_indices[i] = i;
}
// Fill iC_2.
int k = 0;
for (int i = 21; i < 121; ++i)
{
iC_2->cap_indices[k] = i;
++k;
}
// Declare threads.
pthread_t thread1, thread2;
int rT1, rT2;
// Initialize mutex protecting "assembleDeLorean" function.
pthread_mutex_init(&lock, NULL);
// Create & run first thread.
printf("Creating and running thread1.\n");
rT1 = pthread_create(&thread1, NULL, assembleDeLorean, &iC_1);
if (rT1 != 0)
{
printf("Thread 1 could not be created.\n");
return EXIT_FAILURE;
}
printf("Return value of creation of thread1: %d\n", rT1);
// Create & run second thread.
printf("Creating and running thread2.\n");
rT2 = pthread_create(&thread2, NULL, assembleDeLorean, &iC_2);
if (rT2 != 0)
{
printf("Thread 2 could not be created.\n");
return EXIT_FAILURE;
}
printf("Return value of creation of thread2: %d\n", rT2);
// Wait for threads to finish.
printf("Waiting for thread1 to finish...\n");
if (pthread_join(thread1, NULL))
{
printf("An error occured while joining thread1.\n");
return EXIT_FAILURE;
}
printf("Thread1 finished!");
printf("Waiting for thread2 to finish...\n");
if (pthread_join(thread2, NULL))
{
printf("An error occured while joining thread2.\n");
return EXIT_FAILURE;
}
printf("Thread2 finished!");
return EXIT_SUCCESS;
}