CriticalSections in Boost.Threads
-
????????? schrieb:
Wo steht das mit boost::L?
http://boost.org/doc/html/threads/concepts.html#threads.concepts.ScopedLock
-
boost::mutex mutex; boost::mutex::scoped_lock muxexScopedLock(mutex);
-
L schrieb:
boost::mutex mutex; boost::mutex::scoped_lock muxexScopedLock(mutex);Ich hab daraus jetzt mal gefolgert das muxexScopedUnlock dann sowas wie LeaveCriticalSection ist.
Mein Code sieht jetzt so aus:
#include <iostream> #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> void foo() { boost::mutex mutex; for(int i=0; i<100; i++) { boost::mutex::scoped_lock muxexScopedLock(mutex); std::cout << "huhu: " << i << std::endl; boost::mutex::scoped_lock muxexScopedUnlock(mutex); } } void bar() { boost::mutex mutex; for(int i=0; i<100; i++) { boost::mutex::scoped_lock muxexScopedLock(mutex); std::cout << "lala: " << i << std::endl; boost::mutex::scoped_lock muxexScopedUnlock(mutex); } } int main() { boost::thread t1(&foo); // Thread-Objekt t1 erstellen und foo() starten. bar(); // t1 läuft gleichzeitig weiter, während bar() aufgerufen wird! }Mein Ziel ist es, dass sich die couts nicht überschneiden, also z.B. nicht so:
lala: 91
lala: 92
lala: huhu: 92
huhu: 93
huhu: 94
Aber das funktioniert mit dem mutex auch noch nicht. Funktioniert das jetzt so, oder warum ist die Ausgabe nicht richtig?mfg.
-
Du hast wohl den Sinn von scoped_lock überhaupt nicht verstanden.
-
L schrieb:
Du hast wohl den Sinn von scoped_lock überhaupt nicht verstanden.
Ne hab ich nicht. Ich denke das wäre das gleiche wie eine CriticalSection?
mfg.
-
scoped_lock muss in beiden fällen mit dem selben mutex-objekt initilaisiert werden.
-
templäd schrieb:
scoped_lock muss in beiden fällen mit dem selben mutex-objekt initilaisiert werden.
Aso. Stimmt, wie bei der CriticalSection.
Jetzt klappt's auch:#include <iostream> #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> boost::mutex mutex; void foo() { for(int i=0; i<100; i++) { boost::mutex::scoped_lock muxexScopedLock(mutex); std::cout << "huhu: " << i << std::endl; boost::mutex::scoped_lock muxexScopedUnlock(mutex); } } void bar() { for(int i=0; i<100; i++) { boost::mutex::scoped_lock muxexScopedLock(mutex); std::cout << "lala: " << i << std::endl; boost::mutex::scoped_lock muxexScopedUnlock(mutex); } } int main() { boost::thread t1(&foo); // Thread-Objekt t1 erstellen und foo() starten. bar(); // t1 läuft gleichzeitig weiter, während bar() aufgerufen wird! }Vielen Dank!!
mfg.
-
Der Mutex ist die Critical Section. scoped_lock ist nur eine Hilfsklasse die im Konstruktor Lock aufruft und im Destruktor Unlock. Deswegen ist dein Code immer noch falsch.
-
Also die Zeile
boost::mutex::scoped_lock muxexScopedUnlock(mutex);Kannst du ganz weglassen.
-
hmmmmmmmmm schrieb:
Also die Zeile
boost::mutex::scoped_lock muxexScopedUnlock(mutex);Kannst du ganz weglassen.
Achso, okay verstehe.
Also müsste das dann so aussehen:
boost::mutex mutex; void foo() { for(int i=0; i<100; i++) { boost::mutex::scoped_lock muxexScopedLock(mutex); std::cout << "huhu: " << i << std::endl; } }Und wie mach ich das Ganze ohne die Hilfsklasse also direkt mit den Funtkionen von mutex?
mfg.
-
mit boost::mutex::do_lock() und boost::mutex::do_unlock()