使用线程对数组的两半进行排序,但仅对后半部分进行排序
Using threads to sort two halves of an array, but only second half is being sorted
我编写了一个快速程序来对一个数组的两半进行排序,当我测试排序时它对一个数组工作正常,但是当我将数组分成两半并将一半传递给每个线程进行排序时,当它们完成后我打印数组,只有后半部分看起来排序了。我究竟做错了什么?下面是我的排序函数和main.
void *sort(void *object){
struct array_struct *structure;
structure = (struct array_struct *) object;
int *array = structure->partition;
int size = structure->size;
qsort(array, size, sizeof(int), cmpfunc);
printf("Sorted %d elements.\n", size);
}
这是我的主要部分,假设所有包含都很好,编译也很好,这不是我的全部代码,只是与我的问题有关的部分。
int main(int argc, char const *argv[]){
int segments = 2;
pthread_t threads[segments];
int i, *numbers; //iterator i, and pointer to int array 'numbers'
numbers = randomArray(); //return an array of size 50 filled with random ints
for(i = 0; i < segments; i++){
struct array_struct array;//struct to pass as argument on thread creation
int *partition = numbers + (i * (50/segments));//obtain the first index of partition
array.partition = partition; //when i = 0 partition is 0 through 24, when i = 1 partition is 25 through 49
array.size = 50/segments; //25
pthread_create(&threads[i], NULL, sort, (void *) &array);
}
for(i = 0; i < segments; i++){
pthread_join(threads[i], NULL);
}
for(i = 0; i < 50; i++){
printf("%d\n", numbers[i]);
}
pthread_exit(NULL);
}
如果有帮助,这是我的输出:
Sorted 25 elements.
Sorted 25 elements.
19
16
14
16
20
6个
17
13
8个
39
18
0
26
46
45
17
7
46
45
29
15
38
43
19
17
0
2个
4个
7
12
12
12
14
16
17
20
22
22
23
26
29
30
32
33
37
38
38
43
43
46
您正在向第一个线程 array
传递参数,然后立即用第二个线程的参数覆盖该结构的内容。因此,两个线程都将看到第二个线程的参数。
你应该做的是有两个独立的参数。例如,使 array
成为一个包含 2 个结构的数组,并将 &array[0]
传递给第一个线程,将 &array[1]
传递给第二个线程。
此外,在 for 循环的范围内声明 array
是危险的。一旦 for 循环结束,该变量就超出了范围,您的线程可能会读入死变量。您应该在函数级别声明 array
,以便线程访问它时保持活动状态。
我编写了一个快速程序来对一个数组的两半进行排序,当我测试排序时它对一个数组工作正常,但是当我将数组分成两半并将一半传递给每个线程进行排序时,当它们完成后我打印数组,只有后半部分看起来排序了。我究竟做错了什么?下面是我的排序函数和main.
void *sort(void *object){
struct array_struct *structure;
structure = (struct array_struct *) object;
int *array = structure->partition;
int size = structure->size;
qsort(array, size, sizeof(int), cmpfunc);
printf("Sorted %d elements.\n", size);
}
这是我的主要部分,假设所有包含都很好,编译也很好,这不是我的全部代码,只是与我的问题有关的部分。
int main(int argc, char const *argv[]){
int segments = 2;
pthread_t threads[segments];
int i, *numbers; //iterator i, and pointer to int array 'numbers'
numbers = randomArray(); //return an array of size 50 filled with random ints
for(i = 0; i < segments; i++){
struct array_struct array;//struct to pass as argument on thread creation
int *partition = numbers + (i * (50/segments));//obtain the first index of partition
array.partition = partition; //when i = 0 partition is 0 through 24, when i = 1 partition is 25 through 49
array.size = 50/segments; //25
pthread_create(&threads[i], NULL, sort, (void *) &array);
}
for(i = 0; i < segments; i++){
pthread_join(threads[i], NULL);
}
for(i = 0; i < 50; i++){
printf("%d\n", numbers[i]);
}
pthread_exit(NULL);
}
如果有帮助,这是我的输出:
Sorted 25 elements.
Sorted 25 elements.
19 16 14 16 20 6个 17 13 8个 39 18 0 26 46 45 17 7 46 45 29 15 38 43 19 17 0 2个 4个 7 12 12 12 14 16 17 20 22 22 23 26 29 30 32 33 37 38 38 43 43 46
您正在向第一个线程 array
传递参数,然后立即用第二个线程的参数覆盖该结构的内容。因此,两个线程都将看到第二个线程的参数。
你应该做的是有两个独立的参数。例如,使 array
成为一个包含 2 个结构的数组,并将 &array[0]
传递给第一个线程,将 &array[1]
传递给第二个线程。
此外,在 for 循环的范围内声明 array
是危险的。一旦 for 循环结束,该变量就超出了范围,您的线程可能会读入死变量。您应该在函数级别声明 array
,以便线程访问它时保持活动状态。