You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何直接用std::thread调用std::sort,无需中间函数?

如何直接在std::thread中调用std::sort而无需中间函数

问题场景

想让线程直接执行std::sort,避免编写中间包装函数,尝试代码:

std::thread t1(std::sort, data.begin(), data.end());

但在拆分vector为两半分别排序再合并的测试场景中,替换掉可行的中间函数调用后出现编译错误。原测试代码如下:

#include <vector>
#include <thread>
#include <iostream>
#include <algorithm>

void myfunc(std::vector<int>& data) {
    std::sort(data.begin(), data.begin() + data.size() / 2);
    return;
}

int main()
{    
    std::vector<int> data = { 1, 8, 123, 10, -3, 15, 2, 7 };
    std::thread t1(myfunc, std::ref(data)); // 可行
    //std::thread t1(std::sort, data.begin(), data.begin() + data.size() / 2); // 编译失败
    std::sort(data.begin() + data.size() / 2, data.end());
    t1.join();
    std::inplace_merge(data.begin(), data.begin() + data.size() / 2, data.end());

    for (auto x : data)
        std::cout << x << "\n";
}

编译错误信息

error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, std::vector<int>::iterator, __gnu_cxx::__normal_iterator<int*, std::vector<int> >)' 
   16 | d t1(std::sort, data.begin(), data.begin() + data.size() / 2); // doesn't
      |                                                             ^

In file included from /usr/include/c++/11/thread:43,
                 from /tmp/bBJEjs0nrj.cpp:2:
/usr/include/c++/11/bits/std_thread.h:127:7: note: candidate: 'template<class _Callable, class ... _Args, class> std::thread::thread(_Callable&&, _Args&& ...)'
  127 |       thread(_Callable&& __f, _Args&&... __args)
      |       ^~~~~~
/usr/include/c++/11/bits/std_thread.h:127:7: note:   template argument deduction/substitution failed:
/tmp/bBJEjs0nrj.cpp:16:75: note:   couldn't deduce template parameter '_Callable'
   16 | d t1(std::sort, data.begin(), data.begin() + data.size() / 2); // doesn't
      |                                                             ^

In file included from /usr/include/c++/11/thread:43,
                 from /tmp/bBJEjs0nrj.cpp:2:
/usr/include/c++/11/bits/std_thread.h:157:5: note: candidate: 'std::thread::thread(std::thread&&)'
  157 |     thread(thread&& __t) noexcept
      |     ^~~~~~
/usr/include/c++/11/bits/std_thread.h:157:5: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/11/bits/std_thread.h:121:5: note: candidate: 'std::thread::thread()'
  121 |     thread() noexcept = default;
      |     ^~~~~~
/usr/include/c++/11/bits/std_thread.h:121:5: note:   candidate expects 0 arguments, 3 provided

错误原因

std::sort是模板函数,存在多个重载版本(比如默认比较器版本、自定义比较器版本)。当直接传递std::sort给std::thread时,编译器无法自动推导出要使用哪个具体的实例化版本,导致std::thread的模板参数_Callable推导失败。

解决方案

有两种简单方式解决这个问题:

方案1:显式指定std::sort的模板参数

明确告诉编译器要调用的std::sort实例版本:

std::thread t1(&std::sort<std::vector<int>::iterator>, data.begin(), data.begin() + data.size() / 2);

方案2:使用lambda表达式包裹std::sort调用

lambda表达式可以让编译器明确捕获要执行的逻辑,避免重载歧义:

std::thread t1([&data](){
    std::sort(data.begin(), data.begin() + data.size() / 2);
});

修改后的完整代码(lambda版本)

#include <vector>
#include <thread>
#include <iostream>
#include <algorithm>

int main()
{    
    std::vector<int> data = { 1, 8, 123, 10, -3, 15, 2, 7 };
    std::thread t1([&data](){
        std::sort(data.begin(), data.begin() + data.size() / 2);
    });
    std::sort(data.begin() + data.size() / 2, data.end());
    t1.join();
    std::inplace_merge(data.begin(), data.begin() + data.size() / 2, data.end());

    for (size_t i = 0; i < data.size(); ++i) {
        if (i > 0) std::cout << ",";
        std::cout << data[i];
    }
    std::cout << "\n";
}

运行结果

-3,1,2,7,8,10,15,123

内容的提问来源于stack exchange,提问作者mh333

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 08:15:36