English 中文(简体)
2. read熟资源和mut
原标题:Boost thread resources and mutexes

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。

最佳回答

这确实是一个加剧的问题:你先宣布全球变数,从而在全球范围对汇编单位加以界定,从而形成多重定义。

标题中填报extern,在一份申请中加以界定。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签