पर साझा पुस्तकालयों में सिंगलटन के कई उदाहरण मेरे प्रश्न, जैसा कि शीर्षक का उल्लेख है, स्पष्ट है, और मैं विवरण में परिदृश्य का वर्णन करता हूं।लिनक्स
#include <iostream>
#include "singleton.h"
extern "C" void hello() {
std::cout << "singleton.num in hello.so : " << singleton::instance().num << std::endl;
++singleton::instance().num;
std::cout << "singleton.num in hello.so after ++ : " << singleton::instance().num << std::endl;
}
आप देख सकते हैं:
/*
* singleton.h
*
* Created on: 2011-12-24
* Author: bourneli
*/
#ifndef SINGLETON_H_
#define SINGLETON_H_
class singleton
{
private:
singleton() {num = -1;}
static singleton* pInstance;
public:
static singleton& instance()
{
if (NULL == pInstance)
{
pInstance = new singleton();
}
return *pInstance;
}
public:
int num;
};
singleton* singleton::pInstance = NULL;
#endif /* SINGLETON_H_ */
तो, वहाँ निम्नलिखित के रूप में एक प्लगइन hello.cpp कहा जाता है: सिंगलटन निम्नलिखित के रूप में सिंगलटन पैटर्न द्वारा कार्यान्वित नाम के एक वर्ग, फ़ाइल singleton.h में नहीं है कि प्लगइन सिंगलटन को कॉल करता है और सिंगलटन में विशेषता संख्या बदलता है।
पिछले है, एक मुख्य कार्य निम्नलिखित के रूप में सिंगलटन और प्लगइन का उपयोग:
#include <iostream>
#include <dlfcn.h>
#include "singleton.h"
int main() {
using std::cout;
using std::cerr;
using std::endl;
singleton::instance().num = 100; // call singleton
cout << "singleton.num in main : " << singleton::instance().num << endl;// call singleton
// open the library
void* handle = dlopen("./hello.so", RTLD_LAZY);
if (!handle) {
cerr << "Cannot open library: " << dlerror() << '\n';
return 1;
}
// load the symbol
typedef void (*hello_t)();
// reset errors
dlerror();
hello_t hello = (hello_t) dlsym(handle, "hello");
const char *dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol 'hello': " << dlerror() << '\n';
dlclose(handle);
return 1;
}
hello(); // call plugin function hello
cout << "singleton.num in main : " << singleton::instance().num << endl;// call singleton
dlclose(handle);
}
और makefile पीछा कर रहा है:
example1: main.cpp hello.so
$(CXX) $(CXXFLAGS) -o example1 main.cpp -ldl
hello.so: hello.cpp
$(CXX) $(CXXFLAGS) -shared -o hello.so hello.cpp
clean:
rm -f example1 hello.so
.PHONY: clean
हां, तो क्या उत्पादन है? मैंने सोचा था कि वहाँ पीछा कर रहा है:
singleton.num in main : 100
singleton.num in hello.so : 100
singleton.num in hello.so after ++ : 101
singleton.num in main : 101
हालांकि, वास्तविक उत्पादन पीछा कर रहा है:
singleton.num in main : 100
singleton.num in hello.so : -1
singleton.num in hello.so after ++ : 0
singleton.num in main : 100
यह साबित होता है सिंगलटन वर्ग के दो उदाहरणों देखते हैं कि।
क्यों?
क्या आप चाहते हैं कि यह सिंगलटन आपके द्वारा निष्पादित की जा रही प्रक्रिया के लिए सिंगलटन हो? या एक संरक्षित स्मृति के उल्लंघन में "सिस्टम-व्यापी" सिंगलटन हमें प्रदान करता है? – sarnold
प्रश्न, स्पष्ट रूप से कहा गया है, आमतौर पर कुछ भी स्पष्ट है। क्या आप साझा पुस्तकालयों को सिंगलटन साझा करना चाहते हैं या नहीं?क्या आप किसी भी व्यवहार के बारे में सोचते हैं या वास्तव में इसका अनुभव करते हैं? जब तक आप हमें नहीं बताते, तब तक जानने का कोई तरीका नहीं है। – 9000
@ कर्नाल्ड: सिस्टम-व्यापी सिंगलेट्स का एक प्रसिद्ध पैटर्न है जो पता स्थान द्वारा सीमित नहीं है: इसे सर्वर कहा जाता है। लेकिन जब तक मूल पोस्टर अपने कोड के _purpose_ को नहीं बताता है, तब तक यह बताने में मुश्किल होती है कि यह पैटर्न फिट बैठता है या नहीं। – 9000