templates mit boost bind
-
Hi,
ich bekomme folgendes Minimalbeispiel nicht gebaut und verstehe nicht warum.
Ich kompiliere unter visual studio (c++) mit boost. Error ist "Microsoft Compiler funktioniert nicht mehr". Also scheint der Compiler abzustürzen?#include <iostream> #include <boost/thread.hpp> #include <boost/date_time.hpp> template <class TYPE> class A { public: A() {} void workerFunc(unsigned int i) { boost::posix_time::seconds workTime(i*i); std::cout << "Worker: running" << std::endl; // pretend to do something useful... boost::this_thread::sleep(workTime); std::cout << "Worker " << boost::this_thread::get_id() << " finished" << std::endl; } void foo() { boost::thread_group tgrp; //unsigned int nmb_threads = boost::thread::hardware_concurrency(); //std::cout << "Number of threads: " << nmb_threads << std::endl; for (int i = 0; i < 100; i++) std::cout << i << " "; std::cout << std::endl; std::cout << "Now in parallel ..." << std::endl; for (int i = 0; i <= 3; ++i) tgrp.create_thread(boost::bind(&A<TYPE>::workerFunc, i)); // wait for all threads in main tgrp.join_all(); }; }; int main(int argc, char* argv[]) { A<int> myA = A<int>(); myA.foo(); std::cout << "Finished!" << std::endl; return 0; }
-
Die Parameter passen auf jeden Fall nicht. Du willst ja an die Instanz binden. Du bräuchst also irgendsowas:
for (int i = 0; i <= 3; ++i) tgrp.create_thread(boost::bind(&A<TYPE>::workerFunc, this, i));
Hast du ein etwas älteres Visual Studio? Ich glaube, bei mir stürzt der Microsoft Compiler tatsächlich auch ständig ab, wenn die Parameter für boost::bind nicht passen.