簡體   English   中英

使用 std::thread 直接調用 std::sort

[英]Using std::thread to call std::sort directly

如何讓線程直接執行std::sort() ,而不創建中間 function?

就像是:

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

我的 (playground:) 想法是讓每個線程對向量的一半進行排序,然后將其合並:

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

// Type your code here, or load an example.
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)); // works
    //std::thread t1(std::sort, data.begin(), data.begin() + data.size() / 2); // doesn't
    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

所需的 output:

-3,1,27,8,10,15,123

原始代碼的最直接翻譯使用模板實例化:

std::thread t1(std::sort<std::vector<int>::iterator>,
    data.begin(), data.end());

std::sort()是一個 function 模板,因此不能按原樣傳遞給需要具體仿函數(一種行為類似於函數的類型)的 function。 您需要將std::sort轉換為正確的 function 指針類型,或者以某種方式包裝它,讓編譯器確定您嘗試調用哪個版本的std::sort()

處理這個問題的最簡單方法是使用 lambda 表達式來包裝對std::sort()的調用,以便編譯器可以為您執行模板推導。 這看起來像這樣:

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

您可以使用 lambda:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM