Problem mit boost::thread
-
#include <boost/thread/mutex.hpp> #include <boost/thread/thread.hpp> boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe! const size_t myarray_max_size = 100; unsigned long myarray[myarray_max_size]; size_t myarray_size; class counter { public: counter() : count(0) { } int increment() { boost::mutex::scoped_lock scoped_lock(mutex); return ++count; } private: boost::mutex mutex; int count; }; counter c; class myclass { void change_count() { int thread_number = c.increment(); do { const size_t mylocalarray_size = 2; unsigned long mylocalarray[mylocalarray_size]; for (size_t i = 0; i < mylocalarray_size; ++i) { mylocalarray[i] = rand(); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.nsec += 1000; boost::thread::sleep(xt); } boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout << "Thread #" << thread_number << std::endl; for (size_t i = 0; (myarray_size < myarray_max_size) && (i < mylocalarray_size); ++i, ++myarray_size) myarray[myarray_size] = mylocalarray[i]; } while (myarray_size < myarray_max_size); } public: void main() { myarray_size = 0; const int num_threads = 2; boost::thread_group thrds; for (int i = 0; i < num_threads; ++i) thrds.create_thread(&myclass::change_count); do { boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.nsec += 1000; boost::thread::sleep(xt); boost::mutex::scoped_lock scoped_lock(io_mutex); } while (myarray_size < myarray_max_size); thrds.join_all(); std::cout << "Alle beendet" << std::endl; } }; int main() { myclass a; a.main(); char c; std::cin >> c; }macht folgenden Fehler
1>C:\Users\Bla\Documents\Visual Studio 2010\Projects\boost_1_43_0\boost/thread/detail/thread.hpp(59): error C2064: term does not evaluate to a function taking 0 arguments 1> C:\Users\Bla\Documents\Visual Studio 2010\Projects\boost_1_43_0\boost/thread/detail/thread.hpp(58) : while compiling class template member function 'void boost::detail::thread_data<F>::run(void)' 1> with 1> [ 1> F=void (__cdecl myclass::* )(void) 1> ] 1> C:\Users\Bla\Documents\Visual Studio 2010\Projects\boost_1_43_0\boost/thread/detail/thread.hpp(129) : see reference to class template instantiation 'boost::detail::thread_data<F>' being compiled 1> with 1> [ 1> F=void (__cdecl myclass::* )(void) 1> ] 1> C:\Users\Bla\Documents\Visual Studio 2010\Projects\boost_1_43_0\boost/thread/detail/thread.hpp(160) : see reference to function template instantiation 'boost::detail::thread_data_ptr boost::thread::make_thread_info<F&>(void)' being compiled 1> with 1> [ 1> F=void (__cdecl myclass::* )(void) 1> ] 1> C:\Users\Bla\Documents\Visual Studio 2010\Projects\boost_1_43_0\boost/thread/detail/thread_group.hpp(39) : see reference to function template instantiation 'boost::thread::thread<F>(F,boost::thread::dummy *)' being compiled 1> with 1> [ 1> F=void (__cdecl myclass::* )(void) 1> ] 1> main.cpp(148) : see reference to function template instantiation 'boost::thread *boost::thread_group::create_thread<void(__cdecl myclass::* )(void)>(F)' being compiled 1> with 1> [ 1> F=void (__cdecl myclass::* )(void) 1> ](Zeilennummern stimmen nicht)
Das Problem ist, dass die Thread-Funktion eine Memberfunktion meiner Klasse ist. Wie kann ich das lösen?
-
boost::bind
-
Danke! und wie genau damit?
-
dsdffsdf schrieb:
Danke! und wie genau damit?
Es sind mittlerweile 13 Minuten vergangen und du hast immer noch nicht nach "boost::thread mit boost::bind" gegoogelt?
-
Danke, ich hatte nur nach boost::bind gesucht!