pthread_join()无效

时间:2016-06-25 10:38:53

标签: c multithreading pthread-join

我的代码遇到麻烦了。 以下代码启动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);线。 我正在努力寻找解决方案,希望你能帮助我。 感谢。

1 个答案:

答案 0 :(得分:1)

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

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

但是,这个解决方案会带来一系列问题,因为所有线程最终都会执行循环体。您可能需要为shared.cnt != n-1添加额外的支票。这必须以原子方式完成,但在其中一条评论中已经注意到了。

相关问题