c - Pthread代码运行时中途崩溃

标签 c multithreading gcc pthreads

一直在做这个作业并使用pthreadC语言中模拟多线程。该代码使用替代方法找到最大值。它编译得很好,运行也很好,但中途崩溃了。我正在使用 windows 7 64 位。我认为这很重要。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

long n,thread_count;
long* w_array;
long* x_array;

void* Initiate(void* index);//initializes control array W
void* Compare(void* pair);//compares each possible pair of input array x. not finished!

struct Pairs {
    long i,j;
};

int main(int argc, char **argv) {
    pthread_t* thread_handles;
    long thread;
    n = strtol(argv[1],NULL, 10);
    thread_handles = malloc(n*sizeof(pthread_t));

    w_array = malloc(n*sizeof(long));
    x_array = malloc(n*sizeof(long));
/*argv[1] is number of inputs,then comes the array*/

    printf("Number of input values \t %ld \n", n); 
    printf("Input values \t\t X = ");

    for(thread = 0; (thread<n);thread++){
        x_array[thread] = strtol(argv[thread+2], NULL, 10);
        printf(" %ld ", strtol(argv[thread+2], NULL, 10));
    }
    printf("\nAfter Initialization \t W = ");
    for (thread = 0;(thread < n);thread++){
        pthread_create(&thread_handles[thread], NULL, Initiate, (void*)thread);
    }

    for(thread = 0; (thread < n);thread++){
        pthread_join(thread_handles[thread],NULL);
    }

    free(thread_handles);

    /* Problem comes after this section */
    long thread_cmp_count = n*(n-1)/2;
    long t;
    struct Pairs *pair;
    thread_handles = malloc(thread_cmp_count*sizeof(pthread_t));
    thread_cmp_count -= 1;
    for(thread = 0;(thread < n); thread++){
        for(t = thread+1; t < n; t++){
            pair->i = thread;
            pair->j = t;
            pthread_create(&thread_handles[thread_cmp_count--], NULL, Compare, (void*) pair);
        }
    }

    for(thread= 0;(thread<thread_cmp_count); thread++){
        pthread_join(thread_handles[thread], NULL);
    }

    free(thread_handles);


    return 0;

}

这里是原型(prototype)的实现

    void* Initiate(void* index){
        long my_index = (long)index;
        w_array[my_index] = 1;
        printf(" %ld ", w_array[my_index]);
        return NULL;
    }

    void* Compare(void* pair){

        struct Pairs *my_pair = (struct Pairs*)pair;
        long int i_index = (long int) my_pair->i;
        long int j_index = (long int) my_pair->j;
        printf("\nThread %ld, %ld", i_index, j_index);
        return NULL;
    }


Here is snapshot of the output

    C:\Users\Jos\Desktop\c compile>gcc -g -o max max.c -lpthread
    max.c: In function 'main':
    max.c:49:59: warning: cast to pointer from integer of different size [-Wint-to-p
    ointer-cast]
       pthread_create(&thread_handles[thread], NULL, Initiate, (void*)thread);
                                                               ^
    max.c: In function 'Initiate':
    max.c:84:18: warning: cast from pointer to integer of different size [-Wpointer-
    to-int-cast]
      long my_index = (long)index;

                  ^

C:\Users\Jos\Desktop\c compile>max 4 1 2 3 4
Number of input values   4
Input values             X =  1  2  3  4
After Initialization     W =  1  1  1  1

最佳答案

您也需要在下面的代码段中为 struct Pairs *pair; 分配。

    struct Pairs *pair;
    thread_handles = malloc(thread_cmp_count*sizeof(pthread_t));
    thread_cmp_count -= 1;
    for(thread = 0;(thread < n); thread++){
        for(t = thread+1; t < n; t++){
            pair->i = thread;
            pair->j = t;
            pthread_create(&thread_handles[thread_cmp_count--], NULL, Compare, (void*) pair);
        }
    }

使用您的代码,它会取消引用未初始化的指针,因此您会崩溃。

关于c - Pthread代码运行时中途崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20145685/

相关文章:

java - 我如何使用 wait/notifyAll

c - 嵌套函数的实现

C - 解析任意图像文件

将 MIPS 转换为 C,特别是 sltiu

java - JDK问题: interrupt another thread blocked on an IO operation with signal may fail

c - 在 C 中编写方法时遇到问题,包括指针和类型

c - 如何在c99标准下将x获取到C中的pow y

c - Linux (gcc) 上的段错误

c - 释放传递给分配它的函数的指针

c# - 在CLR中,后台线程和前台线程之间有什么区别?