C++ 如何将这两个if语句连接起来?

C++ 如何将这两个if语句连接起来?,c++,multithreading,C++,Multithreading,它们在一个数组的不同部分上工作,因此竞争条件或它使用来自另一个线程的信息不应该有问题。我认为这里需要解决的唯一真正问题是std::thread从std::thread对象的构造函数从其子线程启动,在堆栈上声明std::thread对象时,有条件地执行此操作可能会很困难。在这种情况下,我建议如下: std::thread left (quickSortThreaded1,array, low, index - 1); std::thread right (quickSortThreaded1,ar

它们在一个数组的不同部分上工作,因此竞争条件或它使用来自另一个线程的信息不应该有问题。

我认为这里需要解决的唯一真正问题是
std::thread
std::thread
对象的构造函数从其子线程启动,在堆栈上声明
std::thread
对象时,有条件地执行此操作可能会很困难。在这种情况下,我建议如下:

std::thread left (quickSortThreaded1,array, low, index - 1);
std::thread right (quickSortThreaded1,array, index + 1, high);
left.join();
right.join();
void快速排序(int*数组,大小\u t低,大小\u t高)
{
const size\u t index=分区(数组、低位、高位);
const bool doLeft=(索引>低+1)
const bool doRight=(指数+1<高);
const bool spawnThreads=((doLeft)和&&(doRight));//可能在某个点添加例如&&((high-low)>50000
如果(生成线程)
{
//多线程实现
std::线程左(quickSortThreaded1,数组,低,索引-1);
std::线程右侧(quickSortThreaded1,数组,索引+1,高);
左。join();
对。join();
}
其他的
{
//单线程实现
if(doLeft)快速排序(数组,低位,索引-1);//轴的左侧
if(doRight)快速排序(数组,索引+1,高);//轴的右侧
}
}

稍微重新构造代码,移动条件:

void quickSort (int *array, size_t low, size_t high)
{
    const size_t index = partition(array, low, high);

    const bool doLeft       = (index > low + 1)
    const bool doRight      = (index + 1 < high);
    const bool spawnThreads = ((doLeft)&&(doRight));  // maybe add e.g. &&((high-low)>50000)) at some point
    if (spawnThreads)
    {
       // multi-threaded implementation
       std::thread left (quickSortThreaded1,array, low, index - 1);
       std::thread right (quickSortThreaded1,array, index + 1, high);
       left.join();
       right.join();
    }
    else
    {
       // single-threaded implementation
       if (doLeft)  quickSort(array, low, index - 1); // left side of pivot
       if (doRight) quickSort(array, index + 1, high); // right side of pivot
    }
}

而且转换很简单。

顺便说一句,启动和拆除线程的开销是相当大的,因此,一旦这项工作开始,您可能会发现,除了每个线程都在处理相对大量的数据外,它不会使速度加快很多(甚至减慢速度)。如果是这样,你可能会想输入一些条件逻辑,这样你就可以在有足够数据的时候生成线程,这样就可以了。我正在学习一个初学者C++类,甚至连触碰线程都没有,使用原子和所有我不确定的方法。我会尽力调查的!对于您的第二个评论,是的,教授确实提到使用线程只对大量数据有用
void quickSort (int *array, size_t low, size_t high)
{
    const size_t index = partition(array, low, high);

    const bool doLeft       = (index > low + 1)
    const bool doRight      = (index + 1 < high);
    const bool spawnThreads = ((doLeft)&&(doRight));  // maybe add e.g. &&((high-low)>50000)) at some point
    if (spawnThreads)
    {
       // multi-threaded implementation
       std::thread left (quickSortThreaded1,array, low, index - 1);
       std::thread right (quickSortThreaded1,array, index + 1, high);
       left.join();
       right.join();
    }
    else
    {
       // single-threaded implementation
       if (doLeft)  quickSort(array, low, index - 1); // left side of pivot
       if (doRight) quickSort(array, index + 1, high); // right side of pivot
    }
}
void quickSort (int *array, size_t low, size_t high)
{
    if (low >= high)
        return;
    size_t index = partition(array, low, high);
    quickSort(array, low, index - 1);
    quickSort(array, index + 1, high);
}