Boost::Thread Funktion mit Pointer als Parameter
-
hi,
ich möchte, dass mein main-thread einen worker Thread startet und erst weiterarbeitet, wenn der worker Thread seinen Initialisierungskram abgeschlossen hat (mittels als Pointer übergebener Semaphore)
main-thread:
boost::interprocess::interprocess_semaphore *pSemUsbIntPipeOpen = new boost::interprocess::interprocess_semaphore(0); //start Interrupt thread m_pIntThrd = new boost::thread(boost::bind(&BoardInterfaceUSB::InterruptThread, pSemUsbIntPipeOpen)); //wait at maximum 1 second for the Thread to start if (pSemUsbIntPipeOpen->timed_wait(boost::get_system_time() + boost::posix_time::seconds(1))) { ...worker-Thread:
void BoardInterfaceUSB::InterruptThread(boost::interprocess::interprocess_semaphore *pSemUsbIntPipeOpen) { ... pSemUsbIntPipeOpen->post(); ... }Beim Compilieren bekomme ich diese kryptischen Fehlermeldungen:
pointer to member type 'void (BoardInterfaceUSB::)(boost::interprocess::interprocess_semaphore*)' incompatible with object type 'boost::interprocess::interprocess_semaphore' line 333, external location: ..\..\..\common\lib\boost_1_39_0\boost\bind\mem_fn.hpp C/C++ Problem
mit boost::ref habe ich auch etwas rumexperimentiert. Wenn das hier versuche:
boost::interprocess::interprocess_semaphore semUsbIntPipeOpen(0); m_pIntThrd = new boost::thread(boost::bind(&BoardInterfaceUSB::InterruptThread, boost::ref(pSemUsbIntPipeOpen)));mit
void BoardInterfaceUSB::InterruptThread(boost::interprocess::interprocess_semaphore pSemUsbIntPipeOpen)bekomme ich die Meldung:
no matching function for call to 'get_pointer(const boost::interprocess::interprocess_semaphore&)' RaptorLib line 333, external location: ..\..\..\common\lib\boost_1_39_0\boost\bind\mem_fn.hpp C/C++ Problem
Hat jemand einen Lösungsvorschlag?
Gruß
-
Martin321 schrieb:
Hat jemand einen Lösungsvorschlag?
Als zweiten Parameter an
boost::bindmusst du ein Objekt von TypBoardInterfaceUSBübergeben. Eine Memberfunktion muss schliesslich auf ein Objekt aufgerufen werden. Der Kompiler denkt nun, dasspSemUsbIntPipeOpenein Zeiger zu einem solchen Objekt ist, merkt aber, dass die Typen inkompatibel sind.Noch zwei Fragen an dich:
1. Welche Boost Version verwendest du? In den neueren Versionen istboost::bindnicht mehr nötig im Konstruktor vonboost::thread, da dieser die Argumente selber an einboost::bindweitergibt. Einfach mal in die Boost.Thread Dokumentation schauen, kam vor ein paar Versionen dazu.
2. Wieso eine Interprozess Semaphore? Ist das wirklich eine Synchronisation zwischen zwei Prozessen?Grüssli
-
hi!
Vielen Dank ersteinmal für deine gute Antwort - hat mich schon ein gutes stück weitergebracht.
Als zweiten Parameter an boost::bind musst du ein Objekt von Typ BoardInterfaceUSB übergeben. Eine Memberfunktion muss schliesslich auf ein Objekt aufgerufen werden. Der Kompiler denkt nun, dass pSemUsbIntPipeOpen ein Zeiger zu einem solchen Objekt ist, merkt aber, dass die Typen inkompatibel sind.
Das seh ich ein.
1. Welche Boost Version verwendest du? In den neueren Versionen ist boost::bind nicht mehr nötig im Konstruktor von boost::thread, da dieser die Argumente selber an ein boost::bind weitergibt. Einfach mal in die Boost.Thread Dokumentation schauen, kam vor ein paar Versionen dazu.
Ich benutze die aktuelle 1.39 Version. Ich muss aber gestehen, dass die Doc http://www.boost.org/doc/libs/1_39_0/doc/html/thread.html für mich ziemlich unverständlich und mit zu wenigen Beispielen ist.
Wieso sol ich der Klasse Thread ein Object ohne ein bind vorher geben dürfen?
Thread Constructor with arguments
template <class F,class A1,class A2,...>
thread(F f,A1 a1,A2 a2,...);2. Wieso eine Interprozess Semaphore? Ist das wirklich eine Synchronisation zwischen zwei Prozessen?
Da hast du natürlich recht. Vorher hab ich in solchen Situationen immer mit QSemaphore (QT) gearbeitet. Das geht aber bei meinem aktuellen Projekt leider nicht.
Habe meinen Code jetzt auf Condition Variables umgestellt:
Thread:
void BoardInterfaceUSB::InterruptThread(boost::condition_variable &cond, boost::mutex &mut) boost::lock_guard<boost::mutex> lock(mut); cond.notify_one();Threaderzeugende Methode:
info("Creating USB Interrupt Thread"); boost::mutex mut; boost::unique_lock<boost::mutex> lock(mut); boost::condition_variable cond; InterruptThread(cond, mut); //geht boost::bind(&BoardInterfaceUSB::InterruptThread, this, cond, mut); //Fehlermeldung1 //start Interrupt thread m_pIntThrd = new boost::thread(boost::bind(&BoardInterfaceUSB::InterruptThread, this, cond, mut)); //Fehlemeldung 2 if (cond.timed_wait(lock, boost::get_system_time() + boost::posix_time::seconds(1))) { info("Interrupt Thread successfully started"); } else {Fehlermeldung1: (was mach ich bei dem call by reference falsch?)
g++ -I..\..\..\common\lib\win\PLXSDK\Include -I..\..\..\common\lib\boost_1_39_0 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\BoardInterfaceUSB.o ..\..\src\BoardInterfaceUSB.cpp
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp: In member function 'virtual void RLIB::BoardInterfaceUSB::OpenDevice()':
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp:299: error: 'boost::condition_variable::condition_variable(boost::condition_variable&)' is private
..\..\src\BoardInterfaceUSB.cpp:161: error: within this context
..\..\src\BoardInterfaceUSB.cpp:161: error: initializing argument 3 of 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]'
In file included from ..\..\..\common\lib\boost_1_39_0/boost/thread/mutex.hpp:14,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/detail/thread.hpp:11,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/thread.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread.hpp:13,
from ..\..\src\BoardInterfaceUSB.h:10,
from ..\..\src\BoardInterfaceUSB.cpp:1:
..\..\..\common\lib\boost_1_39_0/boost/noncopyable.hpp: In copy constructor 'boost::mutex::mutex(const boost::mutex&)':
..\..\..\common\lib\boost_1_39_0/boost/noncopyable.hpp:27: error: 'boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)' is private
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/mutex.hpp:25: error: within this context
..\..\src\BoardInterfaceUSB.cpp: In member function 'virtual void RLIB::BoardInterfaceUSB::OpenDevice()':
..\..\src\BoardInterfaceUSB.cpp:161: note: synthesized method 'boost::mutex::mutex(const boost::mutex&)' first required here
..\..\src\BoardInterfaceUSB.cpp:161: error: initializing argument 4 of 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]'
In file included from ..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:1584,
from ..\..\..\common\lib\boost_1_39_0/boost/bind.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/detail/thread.hpp:20,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/thread.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread.hpp:13,
from ..\..\src\BoardInterfaceUSB.h:10,
from ..\..\src\BoardInterfaceUSB.cpp:1:
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp: In function 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]':
..\..\src\BoardInterfaceUSB.cpp:161: instantiated from here
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp:71: error: no matching function for call to 'boost::_bi::valueboost::condition\_variable::value(boost::_bi::valueboost::condition_variable)'
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:116: note: candidates are: boost::_bi::value<T>::value(const T&) [with T = boost::condition_variable]
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:113: note: boost::_bi::valueboost::condition\_variable::value(boost::_bi::valueboost::condition_variable&)
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp:71: error: initializing argument 2 of 'boost::_bi::list3<A1, A2, A3>::list3(A1, A2, A3) [with A1 = boost::_bi::valueRLIB::BoardInterfaceUSB*, A2 = boost::_bi::valueboost::condition\_variable, A3 = boost::_bi::valueboost::mutex]'
In file included from ..\..\..\common\lib\boost_1_39_0/boost/bind.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/detail/thread.hpp:20,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/thread.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread.hpp:13,
from ..\..\src\BoardInterfaceUSB.h:10,
from ..\..\src\BoardInterfaceUSB.cpp:1:
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp: In constructor 'boost::_bi::value<T>::value(const T&) [with T = boost::condition_variable]':
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp:71: instantiated from 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]'
..\..\src\BoardInterfaceUSB.cpp:161: instantiated from here
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:116: error: no matching function for call to 'boost::condition_variable::condition_variable(const boost::condition_variable&)'
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp:302: note: candidates are: boost::condition_variable::condition_variable()
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp:299: note: boost::condition_variable::condition_variable(boost::condition_variable&)Fehlermeldung2:
g++ -I..\..\..\common\lib\win\PLXSDK\Include -I..\..\..\common\lib\boost_1_39_0 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\BoardInterfaceUSB.o ..\..\src\BoardInterfaceUSB.cpp
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp: In member function 'virtual void RLIB::BoardInterfaceUSB::OpenDevice()':
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp:299: error: 'boost::condition_variable::condition_variable(boost::condition_variable&)' is private
..\..\src\BoardInterfaceUSB.cpp:163: error: within this context
..\..\src\BoardInterfaceUSB.cpp:163: error: initializing argument 3 of 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]'
In file included from ..\..\..\common\lib\boost_1_39_0/boost/thread/mutex.hpp:14,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/detail/thread.hpp:11,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/thread.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread.hpp:13,
from ..\..\src\BoardInterfaceUSB.h:10,
from ..\..\src\BoardInterfaceUSB.cpp:1:
..\..\..\common\lib\boost_1_39_0/boost/noncopyable.hpp: In copy constructor 'boost::mutex::mutex(const boost::mutex&)':
..\..\..\common\lib\boost_1_39_0/boost/noncopyable.hpp:27: error: 'boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)' is private
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/mutex.hpp:25: error: within this context
..\..\src\BoardInterfaceUSB.cpp: In member function 'virtual void RLIB::BoardInterfaceUSB::OpenDevice()':
..\..\src\BoardInterfaceUSB.cpp:163: note: synthesized method 'boost::mutex::mutex(const boost::mutex&)' first required here
..\..\src\BoardInterfaceUSB.cpp:163: error: initializing argument 4 of 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]'
..\..\src\BoardInterfaceUSB.cpp:163: error: no matching function for call to 'boost::_bi::bind_t<void, boost::_mfi::mf2<void, RLIB::BoardInterfaceUSB, boost::condition_variable&, boost::mutex&>, boost::_bi::list3<boost::_bi::valueRLIB::BoardInterfaceUSB*, boost::_bi::valueboost::condition\_variable, boost::_bi::valueboost::mutex > >::bind_t(boost::_bi::bind_t<void, boost::_mfi::mf2<void, RLIB::BoardInterfaceUSB, boost::condition_variable&, boost::mutex&>, boost::_bi::list3<boost::_bi::valueRLIB::BoardInterfaceUSB*, boost::_bi::valueboost::condition\_variable, boost::_bi::valueboost::mutex > >)'
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:859: note: candidates are: boost::_bi::bind_t<R, F, L>::bind_t(F, const L&) [with R = void, F = boost::_mfi::mf2<void, RLIB::BoardInterfaceUSB, boost::condition_variable&, boost::mutex&>, L = boost::_bi::list3<boost::_bi::valueRLIB::BoardInterfaceUSB*, boost::_bi::valueboost::condition\_variable, boost::_bi::valueboost::mutex >]
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:854: note: boost::_bi::bind_t<void, boost::_mfi::mf2<void, RLIB::BoardInterfaceUSB, boost::condition_variable&, boost::mutex&>, boost::_bi::list3<boost::_bi::valueRLIB::BoardInterfaceUSB*, boost::_bi::valueboost::condition\_variable, boost::_bi::valueboost::mutex > >::bind_t(boost::_bi::bind_t<void, boost::_mfi::mf2<void, RLIB::BoardInterfaceUSB, boost::condition_variable&, boost::mutex&>, boost::_bi::list3<boost::_bi::valueRLIB::BoardInterfaceUSB*, boost::_bi::valueboost::condition\_variable, boost::_bi::valueboost::mutex > >&)
..\..\src\BoardInterfaceUSB.cpp:163: error: initializing argument 1 of 'boost::thread::thread(F, typename boost::disable_if<boost::is_convertible<T&, boost::detail::thread_move_t<T> >, boost::thread::dummy*>::type) [with F = boost::_bi::bind_t<void, boost::_mfi::mf2<void, RLIB::BoardInterfaceUSB, boost::condition_variable&, boost::mutex&>, boost::_bi::list3<boost::_bi::valueRLIB::BoardInterfaceUSB*, boost::_bi::valueboost::condition\_variable, boost::_bi::valueboost::mutex > >]'
In file included from ..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:1584,
from ..\..\..\common\lib\boost_1_39_0/boost/bind.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/detail/thread.hpp:20,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/thread.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread.hpp:13,
from ..\..\src\BoardInterfaceUSB.h:10,
from ..\..\src\BoardInterfaceUSB.cpp:1:
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp: In function 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]':
..\..\src\BoardInterfaceUSB.cpp:163: instantiated from here
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp:71: error: no matching function for call to 'boost::_bi::valueboost::condition\_variable::value(boost::_bi::valueboost::condition_variable)'
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:116: note: candidates are: boost::_bi::value<T>::value(const T&) [with T = boost::condition_variable]
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:113: note: boost::_bi::valueboost::condition\_variable::value(boost::_bi::valueboost::condition_variable&)
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp:71: error: initializing argument 2 of 'boost::_bi::list3<A1, A2, A3>::list3(A1, A2, A3) [with A1 = boost::_bi::valueRLIB::BoardInterfaceUSB*, A2 = boost::_bi::valueboost::condition\_variable, A3 = boost::_bi::valueboost::mutex]'
In file included from ..\..\..\common\lib\boost_1_39_0/boost/bind.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/detail/thread.hpp:20,
from ..\..\..\common\lib\boost_1_39_0/boost/thread/thread.hpp:22,
from ..\..\..\common\lib\boost_1_39_0/boost/thread.hpp:13,
from ..\..\src\BoardInterfaceUSB.h:10,
from ..\..\src\BoardInterfaceUSB.cpp:1:
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp: In constructor 'boost::_bi::value<T>::value(const T&) [with T = boost::condition_variable]':
..\..\..\common\lib\boost_1_39_0/boost/bind/bind_mf_cc.hpp:71: instantiated from 'boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::)(B1, B2), A1, A2, A3) [with R = void, T = RLIB::BoardInterfaceUSB, B1 = boost::condition_variable&, B2 = boost::mutex&, A1 = RLIB::BoardInterfaceUSB, A2 = boost::condition_variable, A3 = boost::mutex]'
..\..\src\BoardInterfaceUSB.cpp:163: instantiated from here
..\..\..\common\lib\boost_1_39_0/boost/bind/bind.hpp:116: error: no matching function for call to 'boost::condition_variable::condition_variable(const boost::condition_variable&)'
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp:302: note: candidates are: boost::condition_variable::condition_variable()
..\..\..\common\lib\boost_1_39_0/boost/thread/win32/condition_variable.hpp:299: note: boost::condition_variable::condition_variable(boost::condition_variable&)Gruß
Martin
-
Martin321 schrieb:
Wieso sol ich der Klasse Thread ein Object ohne ein bind vorher geben dürfen?
Einfach die Doku lesen:
http://www.boost.org/doc/libs/1_39_0/doc/html/thread/thread_management.html#thread.thread_management.thread.multiple_argument_constructorEffects:
As if thread(boost::bind(f,a1,a2,...)). Consequently, f and each an are copied into internal storage for access by the new thread.Zu deinem neuen Problem:
boost::condition_variableundboost::mutexsind nicht kopierbar. Jedes Argument, welches man allerdings anboost::bindübergibt, wird kopiert. Falls du eine Referenz übergeben willst, musst du diese mitboost::refwrappen.Grüssli
-
hi,
Dravere schrieb:
Martin321 schrieb:
Wieso sol ich der Klasse Thread ein Object ohne ein bind vorher geben dürfen?
Einfach die Doku lesen:
http://www.boost.org/doc/libs/1_39_0/doc/html/thread/thread_management.html#thread.thread_management.thread.multiple_argument_constructorEffects:
As if thread(boost::bind(f,a1,a2,...)). Consequently, f and each an are copied into internal storage for access by the new thread.Zu deinem neuen Problem:
boost::condition_variableundboost::mutexsind nicht kopierbar. Jedes Argument, welches man allerdings anboost::bindübergibt, wird kopiert. Falls du eine Referenz übergeben willst, musst du diese mitboost::refwrappen.Grüssli
Vielen Dank nochmal! Mit deiner Hilfe hab ichs endlich geschafft

Habe es jetzt so gelöst:
m_pIntThrd = new boost::thread(BoardInterfaceUSB::InterruptThread, this, boost::ref(m_bDeviceOpened), boost::ref(bThreadRdy), boost::ref(condUsbIntThrdRdy), boost::ref(mtxUsbIntThread));...beim googlen nach Doku für die Condition variable ist nicht viel bei herum gekommen. Die Boost Doku finde ich immer noch sehr unverständlich und nicht ausreichend. Beim Testen hab ich mir ein Testprogramm geschrieben, welches viellleicht anderen auch hilfreich sein kann.
Achtung: im Testprogramm, ist der InterruptThread keine Memberfunktion, daher fehlt das 'this'!
Wenn man in der 2. Zeile von InterruptThread das return einkommentiert, wird ein Fehler simuliert und das timed_wait auf die condition variable bricht nach 10 Sekunden ab.
#include <iostream> #include <boost/thread.hpp> using namespace std; void InterruptThread(bool &m_bDeviceOpened, bool &bThreadRdy, boost::condition_variable &condUsbIntThrdRdy, boost::mutex &mtxbThreadRdy) { // if (!init()) // Do some critical stuff here // return; //wait until the mainThread unlocks the mutex boost::lock_guard<boost::mutex> lock(mtxbThreadRdy); // mutex is locked now bThreadRdy = true; mtxbThreadRdy.unlock(); cout << "IntThread:: Notifying mainThread\n"; condUsbIntThrdRdy.notify_one(); while (m_bDeviceOpened) { std::cout << "IntThreadRunning\n"; boost::this_thread::sleep(boost::posix_time::milliseconds(2000)); } cout << "IntThread:: Finished!\n"; } int main() { cout << "mainThread: Started" << endl; bool m_bDeviceOpened = true; bool bThreadRdy = false; boost::thread *m_pIntThrd; boost::mutex mtxUsbIntThread; boost::condition_variable condUsbIntThrdRdy; boost::unique_lock<boost::mutex> lock(mtxUsbIntThread); // mutex is locked now //start Thread m_pIntThrd = new boost::thread(&InterruptThread, boost::ref(m_bDeviceOpened), boost::ref(bThreadRdy), boost::ref(condUsbIntThrdRdy), boost::ref(mtxUsbIntThread)); cout << "mainThread: Started Interrupt Thread. Waiting for 1 s\n"; boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); cout << "mainThread: Waiting for condition\n"; // unlock Mtx (implicitly) and wait until being notified or the time expires if (!condUsbIntThrdRdy.timed_wait(lock, boost::get_system_time() + boost::posix_time::seconds(10))) { cout << "mainThread: Time is Up! Starting the Thread failed!\n"; } else { cout << "mainThread: Interrupt Thread successfully started\n"; } boost::this_thread::sleep(boost::posix_time::milliseconds(10000)); //wait 10 secs cout << "mainThread: Stopping Interrupt Thread\n"; m_bDeviceOpened = false; m_pIntThrd->join(); return 0; }Noch etwas zur BoostLib Doku für cond vars: http://www.boost.org/doc/libs/1_39_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref
weil in
prepare_data(); { boost::lock_guard<boost::mutex> lock(mut); data_ready=true; }der lock in Klammern gesetzt wird, wird das Objekt lock am Ende der Klammern zerstört und der dahinterliegende mutex mut automatisch unlocked.
...das hat mich gut eine Stunde gekostet
Viele Grüße