Это моя первая попытка использования мьютекса с параллельной обработкой для получения правильных результатов.У меня есть простая программа, которая вычисляет первые 25 чисел Фибоначчи в параллельных потоках.Когда у меня есть создание и объединение в одном цикле for, результаты верны.Когда я помещаю соединение во второй цикл for, это вывод:
Thread[0] created Thread[1] created The fibonacci of 2= -1073741824
Thread[2] created The fibonacci of 2= -1073741824
Thread[3] created The fibonacci of 3= -2147483648
The fibonacci of 4= 1073741824
Thread[4] created The fibonacci of 4= 1073741824
Thread[5] created The fibonacci of 5= -1073741824
Thread[6] created The fibonacci of 7= 455617800
Thread[7] created The fibonacci of 8= 1984977424
Thread[8] created The fibonacci of 8= 1984977424
Thread[9] created The fibonacci of 10= 1985010191
Thread[10] created The fibonacci of 11= 1985042958
Thread[11] created The fibonacci of 11= 1985042958
Thread[12] created The fibonacci of 12= -324914147
Thread[13] created The fibonacci of 13= 1660128811
Thread[14] created The fibonacci of 15= 1660128811
Thread[15] created The fibonacci of 16= 1660128811
Thread[16] created The fibonacci of 16= 1660128811
Thread[17] created The fibonacci of 18= 1660128811
Thread[18] created The fibonacci of 19= 1660128811
Thread[19] created The fibonacci of 20= -974709674
Thread[20] created The fibonacci of 20= -974709674
Thread[21] created The fibonacci of 22= -974709674
Thread[22] created The fibonacci of 23= -974709674
Thread[23] created The fibonacci of 23= -974709674
The fibonacci of 24= -1949419348
Thread[24] created Thread[0] joined & exited
Thread[1] joined & exited
Thread[2] joined & exited
Thread[3] joined & exited
Thread[4] joined & exited
Thread[5] joined & exited
Thread[6] joined & exited
Thread[7] joined & exited
Thread[8] joined & exited
Thread[9] joined & exited
Thread[10] joined & exited
Thread[11] joined & exited
Thread[12] joined & exited
Thread[13] joined & exited
Thread[14] joined & exited
Thread[15] joined & exited
Thread[16] joined & exited
Thread[17] joined & exited
Thread[18] joined & exited
Thread[19] joined & exited
Thread[20] joined & exited
Thread[21] joined & exited
Thread[22] joined & exited
Thread[23] joined & exited
Thread[24] joined & exited
Можете ли вы сказать мне, почему мьютекс не поддерживает обработку потоков в правильном порядке?Вот программа:
#include<stdio.h> //for printf
#include<stdlib.h> //for malloc
#include<pthread.h> //for threading
#define SIZE 25 //number of fibonaccis to be computed
int *fibResults; //array to store fibonacci results
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
//function takes the index for fibResults as a void pointer
//assigns it to an int variable, calculates and prints
//the results of the fibonacci calculations
void *run(void *ptrarg) //executes and exits each thread
{
pthread_mutex_lock (&mutex1);
int arg = *((int *)ptrarg);
if (arg == 0)
{
fibResults[arg] = 0;
printf("The fibonacci of %d= %d\n", arg, fibResults[arg]);
pthread_mutex_unlock (&mutex1);
pthread_exit(0);
}
else if (arg == 1)
{
fibResults[arg] = 1;
printf("The fibonacci of %d= %d\n", arg, fibResults[arg]);
pthread_mutex_unlock (&mutex1);
pthread_exit(0);
}
else
{
fibResults[arg] = fibResults[arg -1] + fibResults[arg -2];
printf("The fibonacci of %d= %d\n", arg, fibResults[arg]);
pthread_mutex_unlock (&mutex1);
pthread_exit(0);
}
}
//main function that drives the program.
//Loops through created and joining threads
//passes the index value of the fib array
//to the thread run function
int main()
{
pthread_attr_t a;
fibResults = (int*)malloc (SIZE * sizeof(int));
pthread_attr_init(&a);
pthread_t thread_id[SIZE];
for (int i = 0; i < SIZE; i++)
{
pthread_t thread;
pthread_create(&thread_id[i], &a, run,(void*) &i);
printf("Thread[%d] created\t", i);
fflush(stdout);
}
for (int j = 0; j < SIZE; j++)
{
pthread_join(thread_id[j], NULL);
printf("Thread[%d] joined & exited\n", j);
}
return 0;
}