I m 采用简单易读的应用,在这种应用中,我有一台服务器,并有一个焦耳。 因此,情况就是如此:
int main(int argc, char *argv[]) {
appMain app(argc, argv);
return app.exec();
}
appMain::appMain(int c, char **v) : argc(c), argv(v) {
this->Configuration();
}
int appMain::exec() {
appServer Server;
try {
ServerThread = boost::thread(Server);
ServerThread.join();
} catch (...) {
return EXIT_FAILURE;
}
return 0;
}
appMain::Configuration
method just picks up a configuration file and loads it into a struct appConfig
. The thing is, I need this structure to be modifiable from any place it might be used, which means I ve got to use a mutex so as to avoid memory corruption.
Hoping to avoid any possible pointer problems and thread argument passing (which seems kind of painful), I decided to use a global variables, which I declared in appConfig.h:
struct appConfig config;
boost::mutex configMutex;
因此,我添加了我的<代码>extern的声明,其中我使用了这些声明:
appMain.cpp
extern struct appConfig config;
extern boost::mutex configMutex;
appServer.cpp
extern struct appConfig config;
extern boost::mutex configMutex;
appServer::appServer() {
return;
}
void appServer::operator()() {
configMutex.lock();
cout << "appServer thread." << endl;
configMutex.unlock();
}
appServer::~appServer() {
return;
}
在我看来,在汇编时间上似乎没有任何问题,但我获得这一天才:
appServer.o: In function `~appServer :
/usr/include/boost/exception/detail/exception_ptr.hpp:74: multiple definition of `configMutex
appMain.o:/home/eax/CCITP/src/appMain.cpp:163: first defined here
appServer.o: In function `appServer :
/usr/include/boost/exception/exception.hpp:200: multiple definition of `config
appMain.o:/usr/include/c++/4.6/bits/stl_construct.h:94: first defined here
collect2: ld returned 1 exit status
对我如何解决这一问题的任何见解都将受到赞赏。
Julian。