Multithreading – boost:: threads and template functions
•
Java
I tried to run the template function on a separate thread, but IntelliSense (VC 2010 express) kept giving me an error:
Errors only occur after I add templates, so I'm sure it's related to them, but I don't know what it is
The two parameters I pass to boost:: thread are template functions defined as:
template<class F> void perform_test(int* current,int num_tests,F func,std::vector<std::pair<int,int>>* results);
And:
namespace Sort { template<class RandomAccessIterator> void quick(RandomAccessIterator begin,RandomAccessIterator end); } //namespace Sort
I tried calling boost:: thread like this:
std::vector<std::pair<int,int>> quick_results; int current = 0,num_tests = 30; boost::thread test_thread(perform_test,¤t,num_tests,Sort::quick,&quick_results);
Solution
The following version compiles and runs OK for me – the main change is to modify perform_ Test declaration so that parameter 3 is correct in the context you want Also make sure there is code behind the function template
typedef std::vector<std::pair<int,int>> container; template<class F> void perform_test(int* current,void(* func)(typename F,typename F),container* results) { cout << "invoked thread function" << endl; } namespace Sort { template<class RandomAccessIterator> void quick(RandomAccessIterator begin,RandomAccessIterator end) { cout << "invoked sort function" << endl; } } //namespace Sort int main() { container quick_results; int current = 0,num_tests = 30; boost::thread test_thread( &perform_test<container::iterator>,Sort::quick<container::iterator>,&quick_results); test_thread.join(); return 0; };
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码