Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将排序与c+中不工作的pthreads合并+;_C++_Arrays_Pthreads_Mergesort - Fatal编程技术网

C++ 将排序与c+中不工作的pthreads合并+;

C++ 将排序与c+中不工作的pthreads合并+;,c++,arrays,pthreads,mergesort,C++,Arrays,Pthreads,Mergesort,我有这个密码 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <iostream> using namespace std; int N; int* a ; /* structure for array index * used to keep low/high end of sub arrays */ typedef struct Arr {

我有这个密码

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;


int N;
int* a ;
/* structure for array index
 * used to keep low/high end of sub arrays
 */
typedef struct Arr
{
    int low;
    int high;
} ArrayIndex;

void merge(int low, int high)
{
    int mid = (low+high)/2;
    int left = low;
    int right = mid+1;

    int b[high-low+1];
    int i, cur = 0;

    while(left <= mid && right <= high)
    {
        if (a[left] > a[right])
            b[cur++] = a[right++];
        else
            b[cur++] = a[right++];
    }

    while(left <= mid) b[cur++] = a[left++];
    while(right <= high) b[cur++] = a[left++];
    for (i = 0; i < (high-low+1) ; i++) a[low+i] = b[i];
}

void * mergesort(void *a)
{

    ArrayIndex *pa = (ArrayIndex *)a;
    int mid = (pa->low + pa->high)/2;

    ArrayIndex aIndex[N];
    pthread_t thread[N];

    aIndex[0].low = pa->low;
    aIndex[0].high = mid;

    aIndex[1].low = mid+1;
    aIndex[1].high = pa->high;

    if (pa->low >= pa->high) return 0;

    int i;
    for(i = 0; i < N; i++) pthread_create(&thread[i], NULL, mergesort, &aIndex[i]);
    for(i = 0; i < N; i++) pthread_join(thread[i], NULL);

    merge(pa->low, pa->high);

    //pthread_exit(NULL);
    return 0;
}

int main()
{
    int s;
    cout << "\nPlease enter a number of threads:" << endl;
    cout << "-> ";
    cin >> N;
    do
    {
        cout << "\nPlease enter the array size:" << endl;
        cout << "-> ";
        cin >> s;
        if(s%N != 0)
        {
            cout << "\n Number not divisible by: "<< N << endl;
        }
    }
    while (s%N != 0);
    a = new int[s];  // Allocate n ints and save ptr in a.
    for (int i=0; i<s; i++)
    {
        a[i] = rand() % 100 + 1;;    // Initialize all elements to zero.
       // printf ("%d ", a[i]);
    }
    ArrayIndex ai;
    ai.low = 0;
    ai.high = sizeof(a)/sizeof(a[0])-1;
    pthread_t thread;

    pthread_create(&thread, NULL, mergesort, &ai);
    pthread_join(thread, NULL);

    int i;
    for (i = 0; i < s; i++) printf ("%d ", a[i]);
    cout << endl;

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int N;
int*a;
/*数组索引的结构
*用于保持子阵列的低端/高端
*/
typedef结构Arr
{
int低;
int高;
}阵列指数;
无效合并(整数低,整数高)
{
int mid=(低+高)/2;
int左=低;
int right=mid+1;
int b[高-低+1];
int i,cur=0;
而(左低,;
aIndex[0]。高=中;
aIndex[1]。低=中+1;
aIndex[1]。高=帕->高;
如果(pa->low>=pa->high)返回0;
int i;
对于(i=0;ilow,pa->high);
//pthread_exit(NULL);
返回0;
}
int main()
{
int-s;
cout N;
做
{
库特;
如果(s%N!=0)
{
我发现了一个错误:

while(left <= mid && right <= high)
{
    if (a[left] > a[right])
        b[cur++] = a[right++];
    else
        b[cur++] = a[right++];
}

(代码)>(这对我的C++敏感来说是不好的。为什么不给出它们运行的参数的函数?为什么不给它们一个合适的类型?”CastAutiRaFe抱歉冒犯了C++的敏感度,我正在尝试学习新的东西。我知道这不是最好的方法来做合并排序: