C语言中的部分线程排序

C语言中的部分线程排序,c,multithreading,sorting,C,Multithreading,Sorting,我想用一个线程进行部分排序, 我的电流输出是多少 27 12 21 48 15 28 82 69 35 91 13 82 33 35 46 5 35 28 87 95 0 10 20 22 23 30 52 80 86 96 3 8 42 53 67 70 70 71 75 79 5 8 8 18 41 43 70 79 86 88 10 51 56 60 65 84 87 91 94 99 23 25 38 39 40 44 51 56 69 75 20 21 25 29 29

我想用一个线程进行部分排序, 我的电流输出是多少

27 12 21 48 15 28 82 69 35 91 
13 82 33 35 46 5 35 28 87 95 
0 10 20 22 23 30 52 80 86 96 
3 8 42 53 67 70 70 71 75 79 
5 8 8 18 41 43 70 79 86 88 
10 51 56 60 65 84 87 91 94 99 
23 25 38 39 40 44 51 56 69 75 
20 21 25 29 29 38 66 71 73 96 
33 50 9 6 13 27 97 21 70 22 
3 4 6 6 7 15 34 59 63 70 
正如您所看到的,我正在对它进行部分排序,我希望我的输出是这样的(最后没有合并)

如果使用&array[I]并手动输入长度,而不是使用struct,则可以获得正确的输出

这是我目前掌握的代码:

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

int cmpfunc(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

struct values {
    int *arrayptr;
    int length;
};

void *thread_fn(void *a) {
    struct values *start = a;

    qsort(start->arrayptr, start->length, sizeof(int), cmpfunc);
    return (void*)a;
}

int main(int argc, const char *argv[]) {      
    FILE *fp = fopen(argv[3], "r");
    FILE *fp1 = fopen("numS1.dat", "w+");

    //amount of threads
    int threadAmount = atoi(argv[1]);
    //size of input
    int numberAmount = atoi(argv[2]);

    //multidimensional array
    int array[threadAmount][numberAmount / threadAmount];

    for (int i = 0; i < threadAmount; i++)
        for (int j = 0; j < numberAmount / threadAmount; j++)
            fscanf(fp, "%d", &array[i][j]);

    pthread_t threadid[threadAmount];

    for (int i = 0; i < threadAmount; ++i) {
        struct values a = { array[i], numberAmount / threadAmount };

        pthread_create(&threadid[i], NULL, thread_fn, &a);
    }

    for (int i = 0; i < threadAmount; ++i)
        pthread_join(threadid[i], NULL);

    for (int i = 0; i < threadAmount; i++) {
        if (i != 0)
            fprintf(fp1, "\n");
        for (int j = 0; j < numberAmount / threadAmount; j++)
            fprintf(fp1 ,"%d ", array[i][j]);
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
int cmpfunc(常数无效*a,常数无效*b){
返回(*(int*)a-*(int*)b);
}
结构值{
int*arrayptr;
整数长度;
};
空心*螺纹螺纹(空心*a){
结构值*start=a;
qsort(开始->阵列,开始->长度,大小(int),cmpfunc);
返回(无效*)a;
}
int main(int argc,const char*argv[]){
文件*fp=fopen(argv[3],“r”);
文件*fp1=fopen(“numS1.dat”,“w+”);
//线程数
int threadAmount=atoi(argv[1]);
//输入大小
int numberAmount=atoi(argv[2]);
//多维数组
int数组[threadAmount][numberAmount/threadAmount];
对于(int i=0;i
你知道我哪里出错了吗?
我认为这是结构,但我在网上看到的一切都与我正在做的一样。

您正在向新创建的线程传递一个指向自动存储的指针:
结构值
对象在调用范围退出后立即失效,因此新线程无法可靠地访问它。您应该分配
结构值
,并将指向已分配对象的指针作为参数传递给
pthread\u create

for (int i = 0; i < threadAmount; ++i) {
    struct values *a = malloc(sizeof(*a));

    a->arrayptr = array[i];
    a->length = numberAmount / threadAmount;

    pthread_create(&threadid[i], NULL, thread_fn, a);
}

您正在将指向自动存储的指针传递给新创建的线程:
struct values
对象在调用作用域退出后立即失效,因此新线程无法可靠地访问它。您应该分配
结构值
,并将指向已分配对象的指针作为参数传递给
pthread\u create

for (int i = 0; i < threadAmount; ++i) {
    struct values *a = malloc(sizeof(*a));

    a->arrayptr = array[i];
    a->length = numberAmount / threadAmount;

    pthread_create(&threadid[i], NULL, thread_fn, a);
}

堆栈溢出不是调试或代码检查服务。请参见欢迎来到软件开发的有趣阶段。您是否尝试使用调试器?您正在向线程传递指向
a
的指针。然后
a
立即被销毁,因为它是一个超出范围的局部变量。然后过了一段时间,线程实际启动,并查看
a
以前所在的空间。分配结构(使用malloc)并让线程释放它。谢谢大家,malloc,这是我在过去8个月几乎完全使用Java以来完全忘记的一点谢谢!堆栈溢出不是调试或代码复查服务。请参见欢迎来到软件开发的有趣阶段。您是否尝试使用调试器?您正在向线程传递指向
a
的指针。然后
a
立即被销毁,因为它是一个超出范围的局部变量。然后过了一段时间,线程实际启动,并查看
a
以前所在的空间。分配结构(使用malloc)并让线程释放它。谢谢大家,malloc,这是我在过去8个月几乎完全使用Java以来完全忘记的一点谢谢!好的,谢谢!!。malloc就是问题所在。我还将更改比较函数。好的,谢谢!!。malloc就是问题所在。我还将更改比较函数。
int cmpfunc(const void *a, const void *b) {
    return (*(int*)b < *(int*)a) - (*(int*)a < *(int*)b);
}