boost::bind einer boost::function
-
Hallo zusammen,
ich würde gerne eine kleine Klasse schreiben, die die maximale Anzahl an parallel auszuführenden threads beschränkt (boost-threadpool funktioniert nicht unter VS2010). Jetzt treten bei mir bei der Verwendung von boost::bind und boost:function Compiler-Fehler auf, für die ich keine Erklärung finde. Folgender Code fasst mein Problem zusammen:
#include <boost/thread.hpp> #include <boost/bind.hpp> template<typename F> void fu_caller (F threadfun) { //check if mutex is lockable.... //... threadfun(); }; template<typename F> void thread_starter ( F fun) { //funktioniert boost::function<void (F)> f = &fu_caller<F>; //direkter aufruf ohne thread funktioniert f(fun); //bind der boost::function boost::function<void ( void)> f3; //funktioniert nicht, f3 falsch? //f3 = boost::bind<void>(&fu_caller<F>,fun); //funktioniert boost::bind(&fu_caller<F>, fun); //funktioniert nicht boost::thread t(boost::bind(&fu_caller<F>, fun)); }; int add(int a, int b) { return a+b; } int main() { int a = 2; int b = 5; thread_starter(boost::bind(&add,a,b)); return; }Fehlermeldung:
error C2664: 'void (F)': Konvertierung des Parameters 1 von 'int' in 'boost::_bi::bind_t<R,F,L>' nicht möglich d:\libraries\boost_1_46\boost\bind\bind.hpp 253
Ich nutze den vc90-compiler mit VS2010 und boost-1.46.
Ich hoffe, dass mir jemand weiterhelfen kann
-
#include <boost/bind/protect.hpp> // ... thread_starter(boost::protect(boost::bind(&add,a,b)));
-
Dankeschön, so hat es übrigens auch geklappt:
thread_starter<boost::function<void(void)>>(boost::bind(&add,a,b)));