gpt4 book ai didi

c - pthread_join() 不起作用

转载 作者:行者123 更新时间:2023-11-30 15:07:42 25 4
gpt4 key购买 nike

我的代码遇到了问题。以下代码启动 n 个线程,这些线程竞争寻找 n 个不同矩阵的每个对角线的最大值。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <semaphore.h>

void crea_matrix(int **);
void trova_max(void *);

struct bin_sem
{
pthread_mutex_t mutex;
pthread_cond_t cond;
int cnt;
} shared= {PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER };

int n,**matrix,max;

int main ()
{

int i;
srand(time(NULL));
printf("N of matrices: \n");
scanf("%d", &n);
int **matrix = (int **)malloc(3 * sizeof(int *));
for (i=0; i<3; i++)
matrix[i] = (int *)malloc(3 * sizeof(int));

pthread_t *tids;
tids=malloc(n*sizeof(pthread_t));
for(i=0;i<n;i++)
pthread_create(tids+i,NULL,trova_max,matrix);
for(i=0;i<n;i++)
{
pthread_mutex_lock(&shared.mutex);
max=0;
crea_matrix(matrix);
shared.cnt=i;
pthread_cond_signal(&shared.cond);
pthread_mutex_unlock(&shared.mutex);
sleep(1);
}
for(i=0;i<n;i++)
pthread_join(tids[i],NULL);
}

void crea_matrix(int **matrix)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
matrix[i][j]=rand()%101;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}


}


void trova_max(void* arg)
{
int i=0, j=0;
int **arr2d;
arr2d=(int**)arg;
do
{
pthread_mutex_lock(&shared.mutex);
pthread_cond_wait(&shared.cond,&shared.mutex);
printf("\nThread: %ld took control of the mutex...\n",(long int) pthread_self());
for(i=0;i<3;i++)
{

if(arr2d[i][i]>max)
max=arr2d[i][i];
printf("\nFirst diag max: %d\n",max);
}
i=0;

for (j=2;j>=0;j--)
{
printf("\nSecond diag max: %d\n",max);
if (arr2d[i][j]>max)
max=arr2d[i][j];
i++;
}
printf("Max found: %d,matrix n° %d", max,shared.cnt);
pthread_mutex_unlock(&shared.mutex);
} while(shared.cnt !=n-1);


pthread_exit(NULL);

}

问题是,我的大部分代码都可以工作,但是当最大评估完成时,只有 1 个线程可以访问 pthread_exit(NULL);线。我正在努力寻找解决方案,希望你能帮助我。谢谢。

最佳答案

引用这个问题:Is it guaranteed that pthread_cond_signal will wake up a waiting thread?

剩余线程可能仍然阻塞在 pthread_cond_wait 上。在连接之前执行最后的 pthread_cond_broadcast 将释放所有阻塞的线程。

但是,这个解决方案会带来自己的一系列问题,因为所有线程最终都会执行循环体。您可能需要为 shared.cnt != n-1 添加额外的检查。尽管如评论之一所述,这必须以原子方式完成。

关于c - pthread_join() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38027534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com