c++ - thread not working for function with template arguments -


i trying implement multithreaded merge sort, attempt fails compile. here code :

template <class randomaccessiterator> void merge_sort (randomaccessiterator begin,randomaccessiterator end) {     int n = end - begin;     int n1,n2;      if (n == 1)         return;      randomaccessiterator mid = begin + (end-begin)/2;       // merge_sort (begin,mid); // ok     // merge_sort (mid,end);   // ok      thread t1 (merge_sort,begin,mid); // error     thread t2 (merge_sort,mid,end);   // error      t1.join ();     t2.join ();      n1 = mid - begin;     n2 = end - mid;      merge (begin,n1,mid,n2); } 

errors messages gcc (g++ -std=c++11 merge-multithread.cpp):

merge-multithread.cpp: in instantiation of ‘void merge_sort(randomaccessiterator, randomaccessiterator) [with randomaccessiterator = int*]’: merge-multithread.cpp:76:25:   required here merge-multithread.cpp:60:33: error: no matching function call ‘std::thread::thread(<unresolved overloaded function type>, int*&, int*&)’   thread t1 (merge_sort,begin,mid);                                  ^ in file included merge-multithread.cpp:4:0: /usr/include/c++/5.2.0/thread:133:7: note: candidate: template<class _callable, class ... _args> std::thread::thread(_callable&&, _args&& ...)        thread(_callable&& __f, _args&&... __args)        ^ /usr/include/c++/5.2.0/thread:133:7: note:   template argument deduction/substitution failed: merge-multithread.cpp:60:33: note:   couldn't deduce template parameter ‘_callable’   thread t1 (merge_sort,begin,mid);                                  ^ in file included merge-multithread.cpp:4:0: /usr/include/c++/5.2.0/thread:128:5: note: candidate: std::thread::thread(std::thread&&)      thread(thread&& __t) noexcept      ^ /usr/include/c++/5.2.0/thread:128:5: note:   candidate expects 1 argument, 3 provided /usr/include/c++/5.2.0/thread:122:5: note: candidate: std::thread::thread()      thread() noexcept = default; 

merge_sort function template; address of 1 of instantiated functions need specify template arguments:

thread t1 (&merge_sort<randomaccessiterator>,begin,mid); //         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ 

or use static cast:

thread t1 (static_cast<void(*)(randomaccessiterator,randomaccessiterator)>(&merge_sort),begin,mid); 

...or use lambda expression , let compiler automatically deduce types of arguments:

thread t1 ([begin,mid]{merge_sort(begin, mid);}); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -