boost::mutex::scoped_lock
एक म्यूटेक्स लॉक करने के आसपास एक आसान आरएआईआई रैपर है। मैं किसी और चीज के लिए एक समान तकनीक का उपयोग करता हूं: एक आरएआईआई रैपर एक सीरियल डिवाइस से अलग/पुनः संलग्न करने के लिए डेटा इंटरफेस पूछने के आसपास।scoped_lock कैसे "अप्रयुक्त चर" चेतावनी उत्सर्जित करने से बचता है?
क्या मैं समझ नहीं है, हालांकि, यही वजह है कि केवल मेरे वस्तु नीचे कोड mst
— जिसका इन्स्टेन्शियशन और विनाश दुष्प्रभाव — कारणों g++
एक "अप्रयुक्त चर" जबकि l
का प्रबंधन करता है त्रुटि चेतावनी फेंकना की क्या ज़रूरत है में शांत रहना।
क्या आप जानते हैं? क्या तुम मुझे बता सकते हो?
[[email protected] ~]$ cat test.cpp
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
struct MyScopedThing;
struct MyWorkerObject {
void a() { std::cout << "a"; }
void b() { std::cout << "b"; }
boost::shared_ptr<MyScopedThing> getScopedThing();
};
struct MyScopedThing {
MyScopedThing(MyWorkerObject& w) : w(w) {
w.a();
}
~MyScopedThing() {
w.b();
}
MyWorkerObject& w;
};
boost::shared_ptr<MyScopedThing> MyWorkerObject::getScopedThing() {
return boost::shared_ptr<MyScopedThing>(new MyScopedThing(*this));
}
int main() {
boost::mutex m;
boost::mutex::scoped_lock l(m);
MyWorkerObject w;
const boost::shared_ptr<MyScopedThing>& mst = w.getScopedThing();
}
[[email protected] ~]$ g++ test.cpp -o test -lboost_thread -Wall
test.cpp: In function ‘int main()’:
test.cpp:33: warning: unused variable ‘mst’
[[email protected] ~]$ ./test
ab[[email protected] ~]$ g++ -v 2>&1 | grep version
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
यह के साथ "क्या आप जानते हैं? क्या आप मुझे बता सकते हैं?" एक सवाल समाप्त करने के लिए एक छोटे से बेमानी है। : पी – wilhelmtell
@ विल्हेल्मटेल: उनमें से केवल एक अनावश्यक है; दोनों _stylish_ कर रहे हैं;) –
@Tomalak बस एक असंबंधित बात: मैं चर नामित 'वास्तविक कोड में l' :) –