简体   繁体   English

pthread_join()不起作用

[英]pthread_join() not working

I have got troubles with my code. 我的代码有麻烦。 The following code starts n threads that compete to find the max value of each diagonal of n different matrices. 以下代码启动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);

}

The thing is, most of my code works, but when the max evaluations are done, only 1 thread gets access to the pthread_exit(NULL); 问题是,我的大多数代码都可以工作,但是在完成最大评估后,只有1个线程可以访问pthread_exit(NULL); line. 线。 I am struggling to find a solution, Hope you can help me. 我正在努力寻找解决方案,希望您能帮助我。 Thanks. 谢谢。

Referring to this question: Is it guaranteed that pthread_cond_signal will wake up a waiting thread? 关于此问题: 是否保证pthread_cond_signal会唤醒等待的线程?

It is possible that the remaining threads are still blocked on pthread_cond_wait. 其余线程可能仍在pthread_cond_wait上被阻塞。 Doing a final pthread_cond_broadcast before the join will release all the blocked threads. 在联接之前执行最后的pthread_cond_broadcast将释放所有被阻止的线程。

However, this solution will bring its own set of problems as all the threads will eventually execute the body of the loop. 但是,由于所有线程最终都将执行循环主体,因此该解决方案将带来自己的问题。 You may have to add an extra check for shared.cnt != n-1 . 您可能需要为shared.cnt != n-1添加额外的检查。 That has to be done atomically though as noticed in one of the comments. 正如其中一项评论所指出的那样,这必须原子地完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM